Directive isolate scope with ng-repeat scope in AngularJS

前端 未结 2 1487
青春惊慌失措
青春惊慌失措 2020-11-27 09:20

I have a directive with an isolate-scope (so that I can reuse the directive in other places), and when I use this directive with an ng-repeat, it fails to work.

相关标签:
2条回答
  • 2020-11-27 09:38

    Okay, through a lot of the comments above, I have discovered the confusion. First, a couple of points of clarification:

    • ngRepeat does not affect your chosen isolate scope
    • the parameters passed into ngRepeat for use on your directive's attributes do use a prototypically-inherited scope
    • the reason your directive doesn't work has nothing to do with the isolate scope

    Here's an example of the same code but with the directive removed:

    <li ng-repeat="name in names"
        ng-class="{ active: $index == selected }"
        ng-click="selected = $index">
        {{$index}}: {{name.first}} {{name.last}}
    </li>
    

    Here is a JSFiddle demonstrating that it won't work. You get the exact same results as in your directive.

    Why doesn't it work? Because scopes in AngularJS use prototypical inheritance. The value selected on your parent scope is a primitive. In JavaScript, this means that it will be overwritten when a child sets the same value. There is a golden rule in AngularJS scopes: model values should always have a . in them. That is, they should never be primitives. See this SO answer for more information.


    Here is a picture of what the scopes initially look like.

    enter image description here

    After clicking the first item, the scopes now look like this:

    enter image description here

    Notice that a new selected property was created on the ngRepeat scope. The controller scope 003 was not altered.

    You can probably guess what happens when we click on the second item:

    enter image description here


    So your issue is actually not caused by ngRepeat at all - it's caused by breaking a golden rule in AngularJS. The way to fix it is to simply use an object property:

    $scope.state = { selected: undefined };
    
    <li ng-repeat="name in names"
        ng-class="{ active: $index == state.selected }"
        ng-click="state.selected = $index">
        {{$index}}: {{name.first}} {{name.last}}
    </li>
    

    Here is a second JSFiddle showing this works too.

    Here is what the scopes look like initially:

    enter image description here

    After clicking the first item:

    enter image description here

    Here, the controller scope is being affected, as desired.

    Also, to prove that this will still work with your directive with an isolate scope (because, again, this has nothing to do with your problem), here is a JSFiddle for that too, the view must reflect the object. You'll note that the only necessary change was to use an object instead of a primitive.

    Scopes initially:

    enter image description here

    Scopes after clicking on the first item:

    enter image description here

    To conclude: once again, your issue isn't with the isolate scope and it isn't with how ngRepeat works. Your problem is that you're breaking a rule that is known to lead to this very problem. Models in AngularJS should always have a ..

    0 讨论(0)
  • 2020-11-27 09:40

    Without directly trying to avoid answering your questions, instead take a look at the following fiddle:

    http://jsfiddle.net/dVPLM/

    Key point is that instead of trying to fight and change the conventional behaviour of Angular, you could structure your directive to work with ng-repeat as opposed to trying to override it.

    In your template:

        <name-row 
            in-names-list="names"
            io-selected="selected">
        </name-row>
    

    In your directive:

        template:
    '        <ul>' +      
    '            <li ng-repeat="name in inNamesList" ng-class="activeClass($index)" >' +
    '                <a ng-click="setSelected($index)">' +
    '                    {{$index}} - {{name.first}} {{name.last}}' +
    '                </a>' +
    '            </li>' +
    '        </ul>'
    

    In response to your questions:

    • ng-repeat will create a scope, you really shouldn't be trying to change this.
    • Priority in directives isn't just execution order - see: AngularJS : How does the HTML compiler arrange the order for compiling?
    • In Batarang, if you check the performance tab, you can see the expressions bound for each scope, and check if this matches your expectations.
    0 讨论(0)
提交回复
热议问题