В моей пользовательской директиве im получаются значения атрибутов, в моем случае это могут быть числа или массивы, но в моей директиве im получается строковый массив (например: "[1,2]".
Как я могу получить свой массив в атрибуте не как строку?
Посмотреть:
<div my-directive to=[1,2]
директива:
angular.module('myApp')
.directive('myDirective',
[
'$http', '$q','$uibModal',
dir
]);
function dir($http, $q, UserService, $uibModal) {
return {
restrict: 'A',
link: function($scope, element, attrs, controller) {
element.on( 'click', function( evt ){
console.log(attrs.to);
});
}
};
}
Попробуйте следующий подход:
В представлении (init директива & установите параметр директивы)
<div ng-app='demo'>
<demo-directive to-val='[1,2,3,4,5]'></demo-directive>
</div>
О директиве
var demo = angular.module('demo', []);
demo.directive('demoDirective', function($parse) {
return {
restrict: 'E',
template: '<div ng-repeat="val in toVal">{{val}}</div>',
link: function (scope, element, attrs, controller) {
// parse the attribute array to a scope params
scope.toVal = JSON.parse(attrs.toVal);
}
}
});