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)">&times;</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的更多相关文章

  1. [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 ...

  2. Vue Vuex state mutations

    Vuex 解决不同组件的数据共享,与数据持久化 1.npm install vuex --save 2.新建store.js 并引入vue vuex ,Vue.use(Vuex) 3.state在vu ...

  3. AngularJS开发指南3:Angular主要组成部分以及如何协同工作

    AngularJS的主要组成部分是: 启动(startup) - 展示“hello world!” 执行期(runtime) - AngularJS 执行期概览 作用域(scope) - 视图和控制器 ...

  4. [AngularJS] Adding custom methods to angular.module

    There are situations where you might want to add additional methods toangular.module. This is easy t ...

  5. 关于angularjs的$state.go()与ui-sref传参问题

    上次转发过关于angularjs回退的文章,回退用到的还是js的回退功能,直接用history.back();实现功能,当时顺便提了下$state.go()有关路由跳转. 那这回就全面解析下$stat ...

  6. AngularJS进阶(三十四)Angular数据更新不及时问题探讨

    Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ...

  7. AngularJS入门讲解1:angular基本概念

    AngularJS应用程序主要有三个组成部分: 模板(Templates) 模板是您用HTML和CSS编写的文件,展现应用的视图. 您可给HTML添加新的元素.属性标记,作为AngularJS编译器的 ...

  8. AngularJS学习(二)——Angular应用的解析

    本节描述AngularJS应用程序的三个组成部分,并解释它们如何映射到模型-视图-控制器设计模式 模板(Template) 模板是您用HTML和CSS编写的文件,展现应用的视图.您可给HTML添加新的 ...

  9. angularjs 中state.go 跳转并且打开新的浏览器窗口

    包子最近遇到业务人员提的非常无厘头的需求,就是调页面的时候,一定要打开一个新的浏览器窗口...>o<奇葩!!! 但是我的页面都是state.go跳转的呀,我各种百度,发现,貌似state, ...

随机推荐

  1. 【转】一个windows线程池实现

    #ifndef _ThreadPool_H_ #define _ThreadPool_H_ #pragma warning(disable: 4530) #pragma warning(disable ...

  2. ajax请求总是不成功?浏览器的同源策略和跨域问题详解

    场景 码农小明要做一个展示业务数据的大屏给老板看,里面包含了来自自己网站的数据和来自隔壁老王的数据.那么自己网站的数据提供了 http://xiaoming.com/whoami 这样的数据接口隔壁老 ...

  3. Conversion to Dalvik format failed: Unable to execute dex: null

    [2013-11-19 14:18:48 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check th ...

  4. java中的getClass()函数

    Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...

  5. IDEA与Tomcat创建并运行Java Web项目及servlet的简单实现

    创建Web项目 1. File -> New Project ,进入创建项目窗口 2.在 WEB-INF 目录下点击右键, New -> Directory ,创建  classes 和  ...

  6. android模块化app开发笔记-2插件间布局文件共享

    android编程时布局文件,图片资源等都是放在同一个文件夹下,这样照成一个问题就是我们想重用UI布局文件和图片时就还需要其分离这些资料,相信大部分android程序员都遇到过这样的问题,其痛苦程度不 ...

  7. LightOJ 1427 -Repository(ac自动机)

    题意: 求每个模式串在母串中出现的次数 #include <map> #include <set> #include <list> #include <cma ...

  8. [King.yue]Ext.Net 正则表达式用法

    例: .Regex("^[A-Za-z0-9]+$")   //正则表达式 .InvalidText("只能输入英文字符和数字.")); //输入错误提示

  9. 利用原生JavaScript获取样式的方式小结

    来源:http://www.ido321.com/930.html ps:是获取样式,不是设置样式.若没有给元素设置样式值,则返回浏览器给予的默认值.(论坛整理) 1.element.style:只能 ...

  10. wuzhicms内的全局函数--load_class()

    load_class() 可以加载并实例化/coreframe/app/模块名/libs/class/$class.class.php类文件里的对象,如果有扩展类文件EXT_$class.class. ...