[AngularJS] Isolate State Mutations in Angular Components
Managing state is one of the hardest things to do in any application. Angular 2 tackles this problem by making it easy to implement a reactive, uni-directional data flow that favor immutable operations. We are moving in the right direction in Angular 1 by moving our state and logic to models but invariably this raises a question. If we are moving to an immutable world, how do you manage mutable operations like forms in Angular? In this lesson, we are going to learn a surprisingly simple technique to isolate state mutations within a component using component lifecycle hooks.
For example you have a bookmark component, and inside the component, you want to update the bookmark. The solution is you create a component just for update bookmark.
<div class="bookmarks">
<div ng-repeat="bookmark in bookmarksListCtrl.bookmarks | filter:{category:bookmarksListCtrl.getCurrentCategory().name}">
<button type="button" class="close" ng-click="bookmarksListCtrl.deleteBookmark(bookmark)">×</button>
<button type="button" class="btn btn-link" ng-click="bookmarksListCtrl.editBookmark(bookmark)">
<span class="glyphicon glyphicon-pencil"></span>
</button>
<a href="{{bookmark.url}}" target="_blank">{{bookmark.title}}</a>
</div>
<div ng-if="bookmarksListCtrl.getCurrentCategory()">
<button type="button" class="btn btn-link"
ng-if="!bookmarksListCtrl.currentBookmark"
ng-click="bookmarksListCtrl.createBookmark()">
<span class="glyphicon glyphicon-plus"></span>
Create Bookmark
</button>
</div>
<save-bookmark
ng-if="bookmarksListCtrl.currentBookmark"
bookmark="bookmarksListCtrl.currentBookmark"
save="bookmarksListCtrl.onSave(bookmark)"
cancel="bookmarksListCtrl.reset()">
</save-bookmark>
</div>
And inside the save-bookmark component, you can copy the original data, and modify on the copy data:
class SaveController {
  $onChanges() {
    this.editedBookmark = Object.assign({}, this.bookmark);
  }
}
export default SaveController;
This can isolate the component state muataion.
[AngularJS] Isolate State Mutations in Angular Components的更多相关文章
- [Angular] Use Angular components in AngularJS applications with Angular Elements
		When migrating AngularJS (v1.x) applications to Angular you have different options. Using Angular El ... 
- Vue Vuex state mutations
		Vuex 解决不同组件的数据共享,与数据持久化 1.npm install vuex --save 2.新建store.js 并引入vue vuex ,Vue.use(Vuex) 3.state在vu ... 
- AngularJS开发指南3:Angular主要组成部分以及如何协同工作
		AngularJS的主要组成部分是: 启动(startup) - 展示“hello world!” 执行期(runtime) - AngularJS 执行期概览 作用域(scope) - 视图和控制器 ... 
- [AngularJS] Adding custom methods to angular.module
		There are situations where you might want to add additional methods toangular.module. This is easy t ... 
- 关于angularjs的$state.go()与ui-sref传参问题
		上次转发过关于angularjs回退的文章,回退用到的还是js的回退功能,直接用history.back();实现功能,当时顺便提了下$state.go()有关路由跳转. 那这回就全面解析下$stat ... 
- AngularJS进阶(三十四)Angular数据更新不及时问题探讨
		Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ... 
- AngularJS入门讲解1:angular基本概念
		AngularJS应用程序主要有三个组成部分: 模板(Templates) 模板是您用HTML和CSS编写的文件,展现应用的视图. 您可给HTML添加新的元素.属性标记,作为AngularJS编译器的 ... 
- AngularJS学习(二)——Angular应用的解析
		本节描述AngularJS应用程序的三个组成部分,并解释它们如何映射到模型-视图-控制器设计模式 模板(Template) 模板是您用HTML和CSS编写的文件,展现应用的视图.您可给HTML添加新的 ... 
- angularjs 中state.go  跳转并且打开新的浏览器窗口
		包子最近遇到业务人员提的非常无厘头的需求,就是调页面的时候,一定要打开一个新的浏览器窗口...>o<奇葩!!! 但是我的页面都是state.go跳转的呀,我各种百度,发现,貌似state, ... 
随机推荐
- Jmeter教程
			博客园 http://www.cnblogs.com/yangxia-test/category/431240.html 1)分析能力没有LoadRunner详细 2)jmeter不支持IP欺骗,而L ... 
- 一行命令实现Android自动关机
			前几天晚上失眠,实在睡不着觉,于是想用Nexus7听一听小野丽莎的歌,在安静祥和之中睡去(怎么感觉有点...)但是不能让平板总是这么循环播放吧(屋里吐槽Google Play Music),所以在平板 ... 
- Python学习之eventlet.greenpool
			该模块提供对 greenthread 池的支持. greenthread 池提供了一定数量的备用 greenthread ,有效限制了孵化 greenthread 过多导致的内存不足,当池子中没有足够 ... 
- Aggregation(1):Blending、Bagging、Random Forest
			假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ... 
- 用Windows Live Writer发来
			文字 package com.myeclipseide.example.myblog.secure; import com.opensymphony.xwork2.ActionSupport; ... 
- 【Hadoop学习】Super用户以其他用户的名义执行操作
			Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ... 
- 总结的Ubuntu的若干小知识
			一.默认开机直接进入到Ubuntu命令行界面 安装Ubuntu后,开机会默认进入到图形界面,如果不喜欢图形界面,可以通过修改配置,直接进入命令行界面,还行节省100多兆的内存空间.具体方法如下: 修改 ... 
- LUA和C++绑定的一些天然的麻烦
			最近在看Luatinker的源代码,打算自己改(仿写)写搞一个简单的封装C++和LUA的结合的库,结果发现其实麻烦和困惑比想象的多. 比如这些点: 1)有时候使用模板的时候,引用会退化. classt ... 
- JAVA 基础 重新开始
			之前做android开发,因为JAVA基础不牢固的原因,自己对写代码很不自信,很多时候要去找源码或者在相近的代码上修修改改以得到想要的结果,从某种意义上来说这根本算不上真正意义上的程序员.后来看到某位 ... 
- workstack windows to openstack
			https://www.mirantis.com/openstack-portal/express-openstack-portal/migrating-from-vmware-for-windows ... 
