手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >浅谈angularJS 作用域
浅谈angularJS 作用域
摘要:这篇文章主要介绍了浅谈angularJS作用域的相关资料,需要的朋友可以参考下?123456789101112131415161718192...

这篇文章主要介绍了浅谈angularJS 作用域的相关资料,需要的朋友可以参考下

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 !doctype html html ng-app=firstApp meta charset=utf-8 script src=angular-1.3.0.js/script /head div ng-controller=parentCtrl input ng-model=args div ng-controller=childCtrl input ng-model=args script var app=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,子和父之间的值不会再保持同步。

以上所述就是本文的全部内容了,希望大家能够喜欢。

【浅谈angularJS 作用域】相关文章:

浅谈javascript中的闭包

简介AngularJS中$http服务的用法

微信WeixinJSBridge API使用实例

AngularJS 最常用的功能汇总

浅谈JavaScript中的Math.atan()方法的使用

关于JavaScript作用域你想知道的一切

详解AngularJS中的http拦截

浅析JavaScript作用域链、执行上下文与闭包

Webpack 实现 AngularJS 的延迟加载

Angularjs全局变量被作用域监听的正确姿势

精品推荐
分类导航