我是Angular JS的新手。绑定不适用于嵌套控制器
我有几个问题。范围似乎与我的第一个控制器testController
,但不是与我的第二个控制器controlspicy
。
为什么不让我打印出$scope.greeting
?不应该因为我分配了控制器而进行绑定。
这是一个直接指向代码的plunkr链接。
http://plnkr.co/edit/NbED8vXNiZCqBjobrISa?p=preview
<!DOCTYPE html>
<html ng-app="newtest">
<head>
<script data-require="[email protected]*" data-semver="1.3.5" src="https://code.angularjs.org/1.3.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
<script src="spicy.js"></script>
</head>
<body ng-controller="testController">
<h1>Hello Plunker! {{message}}</h1>
<input type="text" name="firstName" ng-model="thetext">
{{double(thetext)}}
<h1 ng-controller="controlspicy">new test</h1>
<h2>{{greeting}}</h2>
</body>
</html>
的script.js
var app = angular.module("newtest", [])
.controller("testController", ["$scope", function($scope) {
$scope.message = "hola";
$scope.double = function(value){
if (value == null){
return 0;
}
return value*2;
};
}]);
spicy.js
变种申请= angular.module( “thespicy”,[]) .controller( “controlspicy” ,[“$ scope”,function($ scope){
$scope.greeting = "hello";
}]);
其他的答案似乎得到,为什么您的具体问题不能正常工作的心脏,但使用上'$ scope'由于JavaScript的原型继承原语在使用嵌套的控制器时,您可能会遇到其他问题。见http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs,并总是试图遵循“点规则” – Claies