Angular JS controller

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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • AngularJS是什么?AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架。首先,它是...
    200813阅读 5,626评论 0 3
  • AngularLearning Angular.js 的一些学习资源 基础 官方:http://docs.angu...
    渴望硕壮的成熟阅读 8,944评论 0 61
  • Angular面试题 一、ng-show/ng-hide与ng-if的区别? 第一点区别是,ng-if在后面表达式...
    w_zhuan阅读 10,923评论 0 26
  • 哈哈哈阿酱
    老坏蛋啊阅读 1,198评论 0 0
  • 不知道为什么,在人群中,总能一眼便认出你, 不知道为什么,你在前面,我就会在后面走的很慢, 不知道为什么,心在纠结...
    雪絮霓漫阅读 1,157评论 0 3

友情链接更多精彩内容