AngularJS: When to pass $scope variable to function

后端 未结 6 1086
滥情空心
滥情空心 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:30

    I think that this is simply a case of inconsistency in the code. I've pondered this question before and came to the following conclusion...

    Rule: Don't pass $scope variables into $scope functions.

    Reading the controller code should be enough code to demonstrate what the function of the component will be. The view should not contain any business logic, just bindings (ng-model, ng-click etc). if something in the view can be made clearer by being moved to the controller, so be it.

    Exception: Allow conditional ng-class statements (e.g. ng-class='{active:$index==item.idx') - putting class conditionals in the controller can be very verbose, and muddies the logic of the controller with ideas from the view. If it's a visual property, keep it in the view.

    Exception: You are working with an item in an ng-repeat. For example:

    
    

    I follow these rules when writing controllers & views, and they seem to work. Hope this helps.

提交回复
热议问题