[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, ...
随机推荐
- Linux 中直接 I/O 机制的介绍
https://www.ibm.com/developerworks/cn/linux/l-cn-directio/ 对于传统的操作系统来说,普通的 I/O 操作一般会被内核缓存,这种 I/O 被称作 ...
- hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10
搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...
- Android中ListView中有button,checkbox,GridView时事件问题
最近做项目,用到了listview的item的一些问题,现在抽空把它们总结一下: 转载请表明出处:http://blog.csdn.net/wdaming1986/article/details/67 ...
- webdriver(python)学习笔记七——多层框架定位与智能等待
多层框架或窗口定位: switch_to_frame() switch_to_window() 智能等待: implicitly_wait() 现在web应用中经常会遇到框架如(frame)或窗口(w ...
- gcc编译器与基本类型3
C语言发展史 1969年贝尔实验室 肯尼斯·蓝·汤普逊,丹尼斯·李奇开发了B语言 ->Unix,New B语言,改名C语言83年提出C语言标准 1989年十二月正式通过C语言标准,C89标准 C ...
- Chapter3:字符串、向量和数组
C++11新特性:范围for(range for) for (declaration: expression) statement vector和数组都是对象的集合,而引用不是对象. vector对象 ...
- 快速开发 jQuery 插件的 10 大技巧(转)
转自:http://www.oschina.net/news/41776/jquery-10-tips 在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模 ...
- 使用DBCC SHOW_STATISTICS展示索引的统计信息
在开始之前搭建演示环境: USE master GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassA', N'U') IS NOT NULL DROP TAB ...
- openstack instance snapshort
从下面的截图 高手应该能够看到openstack基于snapshort的 实例的 备份策略了!!!! 从下面某张截图你也能看到用openstack snapshort备份 也是 不太理想的,首先从“磁 ...
- 第三百零三天 how can I 坚持
今天年会,运气还不错,竟然中了个小奖,一个榨汁机,已经很满足了. 今天加上了她,感觉挺合适,就怕一句话聊不来就带搭不理的了.她很好,懂得知足,不攀比. 弟弟今天把房子首付交了,把贷款办完就算安心了,目 ...