对象的属性值在AngularJS应用中是不会被替换的
Value of property of an object is not replaced in AngularJS app
app.js is:
var app = angular.module('ControllerHierarchyAkaScopesWithinScopes',[]);
app.controller('ParentController',function($scope){ // this $scope is controller specific
$scope.student = {name:"abc"};
});
app.controller('ChildController',function($scope){ // this $scope is controller specific
$scope.setName = function(){
$scope.student.name = "xyz";
};
});
index.html为:
<html ng-app="ControllerHierarchyAkaScopesWithinScopes">
<head>
<script src="angular.js" type="text/javascript"></script>
<script src="ControllerHierarchy.js" type="text/javascript"></script>
</head>
<body>
<div ng-controller="ParentController">
<div ng-contoller="ChildController">
<a ng-click="setName()">click this to change value of $scope.student.name</a>
{{setName()}}
{{student}}
</div>
</div>
</body>
</html>
当我访问index.html
时,它显示{"name":"abc"}
而不是{"name":"xyz"}。
index.html
中setName()
在{{student}}
之前调用了两次,name
的值却没有被替换?这里有个错字:ng- controller ="ChildController"应该是ng-controller
如果修复这个-所有工作
var app = angular.module('ControllerHierarchyAkaScopesWithinScopes',[]);
app.controller('ParentController',function($scope){ // this $scope is controller specific
$scope.student = {name:"abc"};
});
app.controller('ChildController',function($scope){ // this $scope is controller specific
$scope.setName = function(){
console.log('setName', $scope);
$scope.student.name = "xyz";
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ControllerHierarchyAkaScopesWithinScopes">
<div ng-controller="ParentController">
<div ng-controller="ChildController">
<a ng-click="setName()">click this to change value of $scope.student.name</a>
{{student}}
</div>
</div>
</div>
相关文章:
- 将字符串分组为n个字符的块,并应用替换
- 如何在 Sails.js 应用程序中禁用或替换 X-Powered-By 标头
- 为什么不将小写应用于以下替换链中的每个单词
- 为什么我在 JSON.stringify(值,替换器)中的替换器函数在 Angular 应用程序中没有收到键
- 如何将同步 ajax 调用替换为凭据以保持应用状态稳定
- 正则表达式替换而不是替换(谷歌应用脚本)
- 在应用程序级别替换主干同步 - 而不是通过模型或 Xollection
- 如何用谷歌应用程序脚本替换电子表格中的文本
- 在asp.net web应用程序中使用jQuery表替换GridView
- Web应用程序安全:JavaScript代码和数据的替换
- node.js/express应用程序-使用哪个异步方法来替换我对HGET的嵌套调用
- 对象的属性值在AngularJS应用中是不会被替换的
- API/应用程序设计:用新数组替换非常大的数组或修改现有数组
- 通过$http将angular应用替换为另一个应用
- 谷歌应用程序脚本——用卷曲的引号替换直引号
- 如何访问/替换Chrome应用程序中的iframe
- 当页面内容被Ajax响应替换时,为什么不应用jQuery插件
- 我将$scope替换为一个变量,现在我的angular应用程序不能工作了
- 将自定义参数应用于匿名JavaScript替换函数
- 用于调用外部应用程序的NPAPI替换(本机消息传递主机)无法工作