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

                  */
             }
        }

    }]);

私が使用した解決策の 1 つは、この関数でした。これをディレクティブ内に配置し、$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 に答える