angularjs双向绑定

Earlier we looked at AngularJS Isolate scope attribute binding and I hope you got a better idea about isolate scope and how to bind attribute using the same. This is a continuation of the isolate scope series of tutorials and today we are going to look at isolate scope two-way data binding using ” =.”

之前我们看过AngularJS隔离范围属性绑定 ,希望您对隔离范围以及如何使用该属性绑定属性有一个更好的了解。 这是隔离范围教程系列的延续,今天我们将使用“ =”来研究隔离范围双向数据绑定

AngularJS隔离范围双向绑定指令 (AngularJS Isolate Scope Two-way Binding Directive)

The @ character works well for accessing a string value passed into a directive. However, it won’t keep changes made in the directive in-sync with the external or outer scope. You can use “=” character if you need to create a two-way binding between the outer scope and the directive’s isolate scope.

@字符可以很好地访问传递给指令的字符串值。 但是,它不会使在指令中所做的更改与外部或外部范围保持同步。 如果需要在外部作用域和指令的隔离作用域之间创建双向绑定,则可以使用“ =”字符。

We are going to explain this concept with a simple example in the following section.

在下一节中,我们将通过一个简单的示例来解释这个概念。

隔离范围“ =”示例 (Isolate Scope “=” Example)

The following example demonstrates a directive that uses the “=” to explain the two way binding.

下面的示例演示了一个使用“ =”说明双向绑定的指令。

  • We have created a controller MainCtrl and attached ctrlRole equals Development to its scope.我们创建了一个控制器MainCtrl,并将ctrlRole等于Development附加到其作用域。
  • Then create a directive named myEmployee having a custom local scope property role within its isolate scope.然后创建一个名为myEmployee的指令,该指令在其隔离范围内具有自定义的本地范围属性角色

app.js

app.js

var app = angular.module('mainApp', []);app.controller("MainCtrl", function($scope){$scope.ctrlRole = "Development"});app.directive("myEmployee", function() {return {scope:{role:"="},template: 'From Directive : <input type="text" ng-model="role">'};});

The “=” character tells the directive that the object passed into the role property should be bound using a two-way binding. The “=” character make sure that if the external property value changes then the directive’s role property should automatically be updated and vice versa.

“ =”字符告诉指令,传递给角色属性的对象应使用双向绑定进行绑定。 “ =”字符确保如果外部属性值更改,则指令的角色属性应自动更新,反之亦然。

<!DOCTYPE html>
<html><head lang="en"><meta charset="utf-8"><title>AngularJS Isolate Scope</title></head><body><div ng-app="mainApp"><div ng-controller="MainCtrl"><div> From Controller : <input type="text" ng-model="ctrlRole"></div><br><div my-employee role="ctrlRole"></div></div></div><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script><script type="text/javascript" src="app.js"></script></body>
</html>

You can now play with the output. A change in any of the fields automatically reflects in the other one or we can say that two-way binding is happening between them.

您现在可以使用输出了。 任何一个字段的更改都会自动反映在另一个字段中,或者我们可以说它们之间发生了双向绑定。

演示地址

隔离范围“ = attr”示例 (Isolate Scope “=attr” Example)

You can also use  “=attr” property instead of ‘=’ like we used @ and @attr in the previous post.

您也可以使用“ = attr ”属性代替“ =”,就像我们在上 一篇文章中使用@@attr一样。

The following example uses “=attr” instead of “=”.

以下示例使用“ = attr”代替“ =”

app.js

app.js

var app = angular.module('mainApp', []);app.controller("MainCtrl", function($scope){$scope.ctrlRole = "Development"});app.directive("myEmployee", function() {return {scope:{role:"=myrole"},template: 'From Directive : <input type="text" ng-model="role">'};});

You can see the change in this piece of code in the following HTML.

您可以在以下HTML中看到这段代码中的更改。

<div my-employee myrole="ctrlRole"></div>

.
We have used the myrole instead of role as the attribute name.


我们使用了myrole而不是角色作为属性名称。

<!DOCTYPE html>
<html><head lang="en"><meta charset="utf-8"><title>AngularJS Isolate Scope</title></head><body><div ng-app="mainApp"><div ng-controller="MainCtrl"><div> From Controller : <input type="text" ng-model="ctrlRole"></div><br><div my-employee myrole="ctrlRole"></div></div></div><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script><script type="text/javascript" src="app.js"></script></body>
</html>

You will see the same output on your browser.

您将在浏览器中看到相同的输出。

演示地址

That’s all for now and you will see more features in the coming posts.

到此为止,您将在接下来的文章中看到更多功能。

翻译自: https://www.journaldev.com/7572/angularjs-isolate-scope-two-way-binding-example

angularjs双向绑定

angularjs双向绑定_AngularJS隔离范围双向绑定示例相关推荐

  1. angularjs绑定属性_AngularJS隔离范围属性绑定教程

    angularjs绑定属性 We have discussed about the isolate scope and its basic usage in the previous tutorial ...

  2. 02.Vue3 基础语法:常用语法、列表渲染、事件绑定和表单双向绑定

    Vue3 基础语法 1. Vue 中应用和组件的基础概念 createApp createApp 表示创建一个 Vue 应用,存储在 app 变量中. 传入的参数表示,这个应用最外层的组件,应该如何展 ...

  3. 4.AngularJS四大特征之二: 双向数据绑定

    AngularJS四大特征之二: 双向数据绑定 (1)方向一:把Model数据绑定到View上--此后不论何时只要Model发生了改变,则View中的呈现会立即随之改变!实现方法: {{ }}.ngB ...

  4. DIN22 IPO OC双输入双输出单电源供电光电隔离转换器 输入/输出 3000VDC 五隔离 实现正负双向信号的隔离传输

    概述 导轨安装DIN22 IPO OC系列模拟信号隔离放大器是一种将输入信号隔离放大.转换成按比例输出的直流信号混合集成厚模电路.产品广泛应用在电力.远程监控.仪器仪表.医疗设备.工业自控等需要直流信 ...

  5. mysql 绑定参数_MySQL 使用 Perl 绑定参数和列

    SQL 语句通常是动态构建的,用户提供一些输入,并将其内置到语句中. 程序员每次处理用户的输入时都必须谨慎. 它具有一些严重的安全隐患. 动态构建 SQL 语句的推荐方法是使用参数绑定. 绑定参数可以 ...

  6. R语言使用两个分类变量创建双向表(Two Way Table、两个分类变量的频率表)实战: 矩阵的双向表、dataframe的双向表、条形图和马赛克图来可视化频率表

    R语言使用两个分类变量创建双向表(Two Way Table.两个分类变量的频率表)实战: 矩阵的双向表.dataframe的双向表.条形图和马赛克图来可视化频率表 目录

  7. 我的WCF之旅 (11): 再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯...

    在一个基于面向服务的分布式环境中,借助一个标准的.平台无关的Communication Infrastructure,各个Service通过SOAP Message实现相互之间的交互.这个交互的过程实 ...

  8. mysql 查询绑定变量_MySQL高级特性——绑定变量

    从MySQL 4.1 版本开始,就支持服务器端的绑定变量,这大大提高了客户端和服务器端数据传输的效率 介绍 当创建一个绑定变量 SQL 时,客户端会向服务器发送一个SQL语句的原型.服务器端收到这个S ...

  9. 【JetPack】视图绑定 ( ViewBinding ) 各种应用 ( 视图绑定两种方式 | Activity 布局 | 对话框布局 | 自定义组件布局 | RecyclerView 列表布局 )

    文章目录 I . 视图绑定 ( ViewBinding ) 界面的两种方式 II . Activity 界面中 应用 视图绑定 ( ViewBinding ) III . Dialog 对话框界面中 ...

最新文章

  1. python 小游戏500行以内_[宜配屋]听图阁
  2. [转]奇文-闲话操作系统(1/4)
  3. 在行列都排好序的矩阵中找数
  4. cocos2d-x项目101次相遇-安装和环境搭建 -xcode
  5. 在MFC框架下使用osg报内存泄露的解决办法
  6. linux硬盘检测工具,CrazyDiskInfo 硬盘检测工具 Linux版 | 厘米天空
  7. Teams的MessageExtension最新功能:Initiate actions
  8. cv mat保存图片_(七)神秘的Mat
  9. http协议、cookie与session介绍
  10. MySQL 高性能表设计规范
  11. SURF算法之Opencv代码详解
  12. php在线图片编辑,在线图片编辑_html/css_WEB-ITnose
  13. 都在用DevOps,linux基础命令要是还没掌握就out了
  14. Andriod 虚拟机
  15. 林业工程抗旱造林技术
  16. 外国官方网站下载文件速度缓慢
  17. 共阴极和共阳极数码管显示的十六进制代码
  18. 科研必备的14个学术搜索引擎
  19. 数学总体框架及各分类学科框架
  20. 数字unicode码

热门文章

  1. [转载] 在python中pop的用法_python中pop()函数如何使用
  2. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...
  3. [转载] Java static关键字详解
  4. 终极版Servlet——我只能提示您路过别错过
  5. CF1110G Tree-Tac-Toe 博弈论、构造
  6. 步骤1:mybatis工程的创建
  7. Linux基础-编译安装Python
  8. 【转】数字签名与数字证书
  9. Android开发技术周报 Issue#102
  10. POJ 3761 Bubble Sort(乘方取模)