Home  >  Article  >  Web Front-end  >  A brief discussion on the differences of @, =, & instructions in angular

A brief discussion on the differences of @, =, & instructions in angular

青灯夜游
青灯夜游forward
2021-05-08 09:53:202846browse

This article will take you through the differences between @, =, & in angular instructions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on the differences of @, =, & instructions in angular

[Related recommendation: "angular tutorial"]

When the scope in the directive is set to an object , this directive has an independent scope, and AngularJS provides a binding strategy for communicating between the isolated scope and the external scope.

1. @(or @attr)

Use the @ symbol for single-item data binding. The value is always a string, so use {{}}.

In addition, this is also a one-way binding. Changes in external data will be reflected internally, but if the internal data changes, the external data will not change.

Attributes should be connected with -, and its camel case format should be written in the scope.

If the attribute name is not specified through @attr, the local name must be consistent with the name of the DOM attribute.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
	<meta charset="utf-8">
	<title>AngularJS</title>
</head>
<body>
	<div ng-controller="parent">
		<div>
			<input type="text" ng-model="name"/>
		</div>
		<my-name show-name="{{name}}">
		
		</my-name>
	</div>
</body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
	var app = angular.module("myApp", []);
	app.controller("parent", function($scope){
		$scope.name = "Jhon";
	}).directive("myName", function(){
		return {
			restrict:"EA",
			scope:{
				showName: &#39;@&#39;
				// name: &#39;@showName&#39;
			},
			template:&#39;<input type="text" ng-model="showName"/>&#39;,
			// template:&#39;<input type="text" ng-model="name"/>&#39;,
		}
	});
</script>
</html>

2, = (or =attr)

Use = for two-way data binding. Value changes on either side will be reflected on the other side. Because it is a two-way binding, do not use {{}}, otherwise the following demo will report an error.

Attributes should be connected with -, and its camel case format should be written in the scope.

If the attribute name is not specified through @attr, the local name must be consistent with the name of the DOM attribute.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
	<meta charset="utf-8">
	<title>AngularJS</title>
</head>
<body>
	<div ng-controller="parent">
		<div>
			<input type="text" ng-model="name"/>
		</div>
		<my-name show-name="name">
		
		</my-name>
	</div>
</body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript">
	var app = angular.module("myApp", []);
	app.controller("parent", function($scope){
		$scope.name = "Jhon";
	}).directive("myName", function(){
		return {
			restrict:"EA",
			scope:{
				showName: &#39;=&#39;
			},
			template:&#39;<input type="text" ng-model="showName"/>&#39;
		}
	});
</script>
</html>

3. &(or &attr)

& is used to bind external functions.

Attributes should be connected with -, and its camel case format should be written in the scope.

If the attribute name is not specified through @attr, the local name must be consistent with the name of the DOM attribute.

76c82f278ac045591c9159d381de2c57
4704c240457a27d06147e4b0954b83f6
93f0f5c25f18dab9d176bd4f6de5d30e
	4d0d87937f6c83b675e896c64d3eb7c9
	b2386ffb911b14667cb8f0f91ea547a7AngularJS6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
	3cf3733f936ee7ff9ab6793a2dc37e9c
		dc6dce4a544fdca2df29d5ac0ea9906b
			58a4888bfd6bdd4deef6ed9257169b2c
		16b28748ea4df4d9c2150843fecfba68
		9b92361dfcb7f4f2d4da3631b19fe5e4
		
		43dc565517fdd6198f30dfe101a69eb8
	16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956
144fcfe8d86d863df55fad0ee5374c042cacc6d41bbb37262a98f745aa00fbf0
8019067d09615e43c7904885b5246f0a
	var app = angular.module("myApp", []);
	app.controller("parent", function($scope){
		$scope.count = 0;
		$scope.increment = function(){
			$scope.count++;
		};
	}).directive("myName", function(){
		return {
			restrict:"EA",
			scope:{
				showName: '&'
			},
			template:'d05c16aa6e8be371f39f1d631fa39062'
		}
	});
2cacc6d41bbb37262a98f745aa00fbf0
73a6ac4ed44ffec12cee46588e518a5e

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on the differences of @, =, & instructions in angular. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete