I was creating a one way binded directive which had an attribute containing an array as follows:
app.directive('interestBox', function() {
return {
restrict: 'E',
scope: {
interests: '@'
},
templateUrl: '/static/templates/directive_templates/interestbox.html',
link: function(scope, element, attrs) {
scope.interests = scope.$eval(scope.interests); //currently, doing this for converting string to array
console.log(scope.interests);
}
}
})
Here's the markup:
<interest-box interests="{{profile_data.interests}}">
</interest-box>
Controller:
$scope.profile_data.interests = [1, 2, 3, 4];
First off, the reason I am using @
and not =
is that I don't need a two way binding between my controller and directive(am I correct regarding this assumption?)
But as you have it, @
parses the DOM value at the time into a string. Now, since my attribute 'interests' is an array, I need to convert it into an array from a string(which @
converts it into) in the directive. For that I am doing:
$scope.interests = scope.$eval(scope.interests);
And this doesn't feel right to me. What would be the best way to get my array from the string?