I have a javascript array as follows:
['create', 'delete', 'deleteList', 'fetch', 'fetchAll', 'patch', 'replaceList', 'update']
I am trying to create an angualrjs directive that takes that array and creates a list of checkboxes that uses these keywords in the array and uses true or false based on whether they are checked or not.
What I did so far is this:
Created the following html:
<span ng-repeat="method in option.methods">
<permission-method method="method"></permission-method>
</span>
and the following javascript:
'use_strict';
var rcDirectives = angular.module('rcDirectives', []);
rcDirectives.directive('permissionMethod', function(){
return {
restrict: 'E', // element
scope: {
method: '='
},
templateUrl: '<label>{{method}} <input type="checkbox" value="{{method}}"><label>'
};
});
Not sure how to continue from here...
EDIT1:
To clearify, let's assume I have that array of strings in a json I call options. For example:
option =
{
name: "some name",
methods = ['fetch', 'fetchAll']
}
Now, when I use this directive, it will create the checkbox list using the methods array with only the fetch and fetchAll checked, and also allow me to change the methods by adding or removing items from the array.
In general this should work for any array, given a premade list of strings.
MY SOLUTION:
So here's how I solved it:
I've created an object instead of array like as follows:
{ create : false, 'delete' : false, deleteList : false, fetch : true, fetchAll : true, patch : false, replaceList : false, update : false }
And on the directive I used this:
rcDirectives.directive('permissionData', function(){
return {
restrict: 'E', // element
scope: {
data: '='
},
templateUrl: 'permissionData.html',
};
});
This is permissionData.html contenct:
<div>
<span ng-repeat="(key, value) in data">
<label>{{key}}
<input type="checkbox" ng-model="data[key]">
</label>
</span>
</div>
methods= [ 'fetch': true, 'fetchAll': true, 'otherMethod': false] Then you can just useng-show="method"to determine whether that method is usable in the DOM. I would need to see more code to give you a real demonstration.