Angular.JS: views sharing same controller, model data resets when changing view
See the angular docs on how to use controllers correctly.
http://docs.angularjs.org/guide/dev_guide.mvc.understanding_controller
Controllers are disposed when changing routes. This is good behavior since you should not rely on controllers to carry data between views. It's best to create a service to handle that data.
当改变路线时,控制器被布置。这是好的行为,因为你不应该依赖控制器在视图之间携带数据。最好创建一个服务来处理这些数据。
今天遇到的问题:
打开一个新的页面更改 bio的值,如果用一个新的view的话,在两个controller之间传递数据就会是一个问题;
angularJS也并不鼓励在两个controller之间传递数据,可以写个service让controller共享$scope.
但我最终的解决办法是:
将两个div显示在一个页面上:
html:
<div id="editProfile" ng-show="showEdit"> </div>
<div id="changeBio" ng-show="showChangeBio"> </div>
js:
$scope.editBio= function () {
$scope.showEdit = false;
$scope.showChangeBio = true;
};
$scope.goEdit= function () {
$scope.showEdit = true;
$scope.showChangeBio = false;}
接下来要思考的问题:
什么时候需要创建一个新的view?
用这样两层div切换有什么弊端吗?
ng repeat ng if last
skip last item in ng-repeat
<div ng-repeat-start="item in items"
ng-if="!$last" big> big item {{item}}
</div>