浅谈angularJS 作用域
<!doctypehtml>
<htmlng-app="firstApp">
<head>
<metacharset="utf-8">
<scriptsrc="angular-1.3.0.js"></script>
</head>
<body>
<divng-controller="parentCtrl">
<inputng-model="args">
<divng-controller="childCtrl">
<inputng-model="args">
</div>
</div>
<script>
varapp=angular.module('firstApp',[]);
app.controller('parentCtrl',function($scope){
$scope.args='123';
}).controller('childCtrl',function($scope){
});
</script>
案例说明:
虽然在childCtrl中没有定义具体的args属性,但是因为childCtrl的作用域继承自parentCtrl的作用域,
因此,childCtrl通过原型链到父作用域args属性并设置到input中。且在父input中输入值自己动同步到子input中
但是反之不行。即子中修改,无法改变父中的值,且导致父修改后子也不同步了,原因:在子作用域input输入内容时,
因为HTML代码中model明确绑定在childCtrl的作用域中,因此AngularJS会为childCtrl生成一个args原始类型属性。
根据AngularJS作用域继承原型机制,childCtrl在自己的作用域找到args属性值,故就不从父中查找args值。
导致最终子作用域有args,父作用域有args,子和父之间的值不会再保持同步。
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下