Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  157] [ 3]  / answers: 1 / hits: 15507  / 11 Years ago, sat, november 2, 2013, 12:00:00

I created a directive in angular where its scope is not isolated



<div ng-controller=myController>
<ul>
<li myDirective=model[1].list >a lot more things will happen inside here</li>
</ul>
</div>


CONTROLLER:



app.controller(myController,[$scope,function($scope){ 
$scope.model = [
{
list:[ object1,object2,object3 ]
},
{
list:[ object4,object5,object6 ]
},
{
list:[ object7,object8,object9 ]
}
]
}]);


Directive:



    app.directive(myDirective,[function(){
return {
scope:true,
controller:function($scope,$element,$attrs){
/*
How do I directly be able to manipulate from inside here what is assigned

on the $attrs.myDirective

without ISOLATING the scope of the directive

*/
}
}

}]);


one of the solution i used was this function i put this inside the directive and process the string assigned to the $attrs.myDirective it works well with model.list
but with model[2].list it doesnt because its not built for it. How do I modify this function or are there better solutions for this problem...



    $scope.rediks = function(string)
{
var scope = $scope;
var scopeSplit = string.split('.');
for (i = 0; i < scopeSplit.length - 1; i++)
{
scope = scope[scopeSplit[i]];
if (scope == undefined) return;
}
return scope[scopeSplit[scopeSplit.length - 1]];
}


Thank you very much,


More From » string

 Answers
4

Use the $parse service (docs) to get the value pointed to by the attribute:



controller: function($scope,$element,$attrs,$parse) {
var value = $parse($attrs.myDirective)($scope);
// for the above case, value = [ object4,object5,object6 ]
}

[#74548] Thursday, October 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brianaclaras

Total Points: 23
Total Questions: 106
Total Answers: 111

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;