angularjs绑定属性

We have discussed about the isolate scope and its basic usage in the previous tutorial. We used {} (object hash) to isolate the scope and it is useful when creating reusable components. Therefore, it will not inherit or modify the data in the parent scope.

在上一教程中,我们已经讨论了隔离范围及其基本用法。 我们使用{}(对象哈希)来隔离范围,这在创建可重用组件时非常有用。 因此,它将不会继承或修改父作用域中的数据。

Since isolate scope takes an object hash, we can define a set of local properties. These properties are useful for aliasing values for templates.

由于隔离作用域采用对象哈希,因此我们可以定义一组局部属性。 这些属性对于别名为模板的值很有用。

  • @ or @attr – bind a local scope property to the value of DOM attribute.@@attr –将本地范围属性绑定到DOM属性的值。
  • = or =attr – set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute.==attr –在本地范围属性和通过attr属性的值定义的名称的父范围属性之间建立双向绑定。
  • & or &attr – provides a way to execute an expression in the context of the parent scope.&&attr –提供一种在父作用域的上下文中执行表达式的方法。

In this tutorial, we will be discussing about the isolate scope attribute binding with an example.

在本教程中,我们将通过一个示例讨论隔离范围属性绑定。

隔离范围属性 (Isolate Scope Attribute)

The @ or @attr is used to bind a local scope property to the value of DOM attribute. We are going to discuss this property in detail with an example.

@@attr用于将本地scope属性绑定到DOM属性的值。 我们将通过一个示例详细讨论此属性。

Let’s create a directive myEmployee to display the role of an employee and isolate the scope with an empty object. We are not using the '@' property in the following example. We have used a link function to set the scope property to the value of the attribute role in the example.

让我们创建一个指令myEmployee,以显示雇员的角色并用空对象隔离作用域。 在以下示例中,我们不使用'@'属性。 在示例中,我们使用link函数将scope属性设置为attribute 角色的值。

var app = angular.module('mainApp', []);app.directive("myEmployee", function() {return {restrict: 'E',scope:{},template: '<h3> Employee Role : {{role}} </h3>',link: function(scope,element,attrs){scope.role = attrs.role;}};
});

We have used the myEmployee directive in the following HTML code and passed a string value to the role attribute

我们在以下HTML代码中使用了myEmployee指令,并将字符串值传递给role属性

<div ng-app="mainApp"><my-employee role="development"></my-employee></div>

You will see the following output in your browser.

您将在浏览器中看到以下输出。

使用隔离范围'@' (Using Isolate Scope ‘@’)

We can get rid of the link function in the above example by using isolate scope '@' property to bind the value of the role attribute.

我们可以通过使用隔离范围'@'属性绑定角色属性的值来摆脱上述示例中的链接函数。

You can bind the scope property to the attribute value simply by using the following syntax.

您可以使用以下语法简单地将scope属性绑定到属性值。

scope:{attribute:"@"
}

The following example demonstrates the Isolate scope attribute binding.

下面的示例演示隔离范围属性绑定。

var app = angular.module('mainApp', []);app.directive("myEmployee", function() {return {restrict: 'E',scope:{role:"@"},template: '<h3> Employee Role : {{role}} </h3>'};
});
<!DOCTYPE html>
<html><head lang="en"><meta charset="utf-8"><title>AngularJS Isolate Scope</title></head><body><div ng-app="mainApp"><my-employee role="development"></my-employee></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 in your browser.

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

使用隔离范围'@attr' (Using Isolate Scope ‘@attr’)

You can also use  @attr property instead of '@'  so that you can differentiate the scope properties and attributes by their names. The following example demonstrates this usage and you will see the same output on your browser.
We have set the attribute name of myEmployee directive to myrole. By using @attr, the value of myrole attribute will set to the scope property. 

您也可以使用@attr属性而不是'@'以便您可以通过名称区分作用域属性和属性。 下面的示例演示了这种用法,您将在浏览器中看到相同的输出。
我们已经将myEmployee指令的属性名称设置为myrole 。 通过使用@attrmyrole属性的值将设置为scope属性。  

var app = angular.module('mainApp', []);app.directive("myEmployee", function() {return {restrict: 'E',scope:{role:"@myrole"},template: '<h3> Employee Role : {{role}} </h3>'};
});
<!DOCTYPE html>
<html><head lang="en"><meta charset="utf-8"><title>AngularJS Isolate Scope</title></head><body><div ng-app="mainApp"><my-employee myrole="development"></my-employee></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>

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

到此为止,我们将在接下来的文章中看到更多的隔离范围功能。

翻译自: https://www.journaldev.com/7568/angularjs-isolate-scope-attribute-bindingtutorial-example

angularjs绑定属性

angularjs绑定属性_AngularJS隔离范围属性绑定教程相关推荐

  1. angularjs双向绑定_AngularJS隔离范围双向绑定示例

    angularjs双向绑定 Earlier we looked at AngularJS Isolate scope attribute binding and I hope you got a be ...

  2. angularjs绑定属性_AngularJS隔离范围绑定表达式教程

    angularjs绑定属性 We have seen how to use local scope properties to pass values into a directive as stri ...

  3. Vue Angular 双向绑定检测不到对象属性的添加和删除

    由于ES5的限制 Vue  && Angular 双向绑定检测不到对象属性的添加和删除  还有数组增加索引.这些改变不会触发change事件.Vue是因为实例化的时候已经把各个属性都s ...

  4. python methodtype_Python的实例定属性和方法或类绑定方法

    一.给实例对象绑定属性和方法: 1.给实例绑定属性:先定义一个Student类 #!/usr/bin/python class Student(object): pass 然后绑定属性: s = St ...

  5. C# WCF WinCE 解决方案 错误提示之:已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性...

    C# WCF WinCE 解决方案 错误提示之:已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性 网上的解决方案 ...

  6. 二、Vue 属性绑定、v-model的原理、绑定class、绑定style

    一.属性绑定和v-model的原理 v-bind: 可以绑定属性 例如 value属性 class属性 style属性等 也可以直接简写成 : <!DOCTYPE html> <ht ...

  7. 【WPF】WPF通过RelativeSource绑定父控件的属性

    1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...

  8. 2.VUE模板语法,属性,样式,事件绑定,事件修饰符

    一.概述 Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.     Vue.js 的核心是一个允许你采用简洁的模板语法来声明式的将数据渲染 ...

  9. 如何在AngularJS的ng-options中设置value属性?

    本文翻译自:How do I set the value property in AngularJS' ng-options? Here is what seems to be bothering a ...

最新文章

  1. 阅读笔记,软件需求分析
  2. bzoj1095 [ZJOI2007]Hide 捉迷藏
  3. Spring和SpringMVC父子容器关系初窥
  4. 快速集成二维码扫描,使用最新版本的zxing(2017.11.10抽取zxing代码)
  5. 协方差矩阵、相关矩阵的详细说明
  6. Elasticsearch---Analyzer(分析器)
  7. Sybase数据库整体迁移纪要
  8. ValueError: operands could not be broadcast together with shapes、numpy广播错误
  9. 小糊涂家装预算软件 v8.0 怎么用
  10. VS2017使用Visual Leak Detector
  11. 如何在 JIRA 中创建自己的仪表板(Dashboard)
  12. 计蒜客 - 德克萨斯长角牛(最短路)
  13. 【个人研究】21世纪西方运用脑电(EEG)的音乐研究动向分析(一)
  14. AlwaysOn实现主从切换
  15. mac制作铃声 我要变好看
  16. 日常用语--打电话(整理)
  17. Google的免费午餐
  18. 攻防世界misc新手之give_you_flag
  19. sharding-jdbc4.1.1 分库分表后 mysql查询优化(count)
  20. 如何判断场效应晶体管方向,学会这几步轻松搞定

热门文章

  1. ASP.NET配置FCKeditor文本编辑器
  2. 不可磨灭的记忆 CPU发展史经典回顾
  3. 懒惰是人类进步的动力,勤奋是实现偷懒的途径
  4. C#利用CDOSYS组件发邮件的一些小结
  5. [转载] python执行shell命令的几种方法
  6. [转载] python将图片进行base64编码, 解码
  7. 深度学习基础(基础知识0)
  8. CodeForces 551E(平方分割
  9. 用Java实现非阻塞通信
  10. SharePoint:扩展DVWP - 第34部分:使用图标形式的表单操作链接