Why can't I overwrite the value of a variable like this?

前端 未结 2 1316
情书的邮戳
情书的邮戳 2020-12-11 17:28

I\'m trying to figure out why I\'m having trouble overwriting a value passed to an angularJS directive via an isolate scope (@). I try to overwrite the value o

2条回答
  •  悲哀的现实
    2020-12-11 18:05

    Apparently it has something to do with one-way binding of the scope index value. So Angular won't update scope.index (or this.index in case of bindToController: true) because scope is configured as

    scope: {
        index: '@'
    },
    

    If you change it to two-way binding like:

    scope: {
        index: '='
    },
    

    It will work:

    
    

    Demo: http://plnkr.co/edit/kq16cpk7gyw8IE7HiaQL?p=preview

    UPD. @pankajparkar made a good point that updating value in the next digest fixed the issue. This approach for the problem then is closer then what I did in this answer.

提交回复
热议问题