0

我在角度创建了一个指令,它的范围不是孤立的

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

控制器:

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

指示:

    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

                  */
             }
        }

    }]);

我使用的解决方案之一是这个函数我把它放在指令中并处理分配给 $attrs.myDirective 的字符串它适用于“model.list”但使用“model[2].list”它不是因为它不是为它而建。如何修改此功能或有更好的解决方案来解决此问题...

    $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]];
    }

非常感谢,

4

1 回答 1

7

使用$parse服务 ( docs ) 获取属性指向的值:

controller: function($scope,$element,$attrs,$parse) {
    var value = $parse($attrs.myDirective)($scope);
    // for the above case, value = [ "object4","object5","object6" ]
}
于 2013-11-02T12:12:33.837 回答