Where Should Model State Be Stored In Angular.js

后端 未结 3 1764
死守一世寂寞
死守一世寂寞 2020-12-12 14:38

I\'m finding Angular\'s use of models confusing. Angular seems to take the approach that a model can be anything you like - I.E. Angular does not include an explicit model c

3条回答
  •  既然无缘
    2020-12-12 14:40

    Angular does not have an opinion on how you store what you call "model objects". The Angular controller $scope exists solely as a "view model" for the purposes of managing your UI. I suggest separating these two concepts in your code.

    If you want the nicety of Angular scope change notification ($watch), you can use a scope object to store your model data if you wish (var myScope = $rootScope.$new()). Just don't use the same scope object to which your UI is bound.

    I recommend writing custom services to this end. So the data flow goes like this:

    AJAX --> Custom Service --> Model Scope Object --> Controller --> UI Scope Object --> DOM

    Or this:

    AJAX --> Custom Services --> Plain old JavaScript Objects --> Controller --> UI Scope Object --> DOM

提交回复
热议问题