AngularJS: When to pass $scope variable to function

后端 未结 6 1081
滥情空心
滥情空心 2020-12-14 01:56

I am using the TodoMVC app to get better with the AngularJS framework. In the index.html on lines 14-16 you see this:

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 02:20

    The Zen of Angular suggests:

    Treat scope as read only in templates
    Treat scope as write only in controllers
    

    Following this principle, you should always call functions explicity with parameters from the template.

    However, in any style you follow, you do have to be careful about priorities and order of execution of directives. In your example, using ng-model and ng-click leaves the order of execution of the two directives ambiguous. The solution is using ng-change, where the order of execution is clear: it will be executed only after the value changes.

提交回复
热议问题