我正面临使用angualrjs $ scope和$ rootscope打印文本的问题。 请找到下面的代码,让我知道我错误的地方。
<body ng-app="x">
<div ng-controller="ctrl1">
Hello {{name2}}
</div>
<div ng-controller="ctrl2">
Hi {{name1}} How {{name3}}
</div>
<script>
var app= angulr.module("x",[]);
app.controller("ctrl1" ,['$scope', '$rootScope',function($scope, $rootscope){
$scope.name2 = "Sir";
$rootscope.name3 = "are you!";
}]);
app.controller("ctrl2", function($scope){
$scope.name1 = "madam";
});
</script>
</body>
答案 0 :(得分:0)
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="x">
<div ng-controller="ctrl1">
Hello {{name2}}
</div>
<div ng-controller="ctrl2">
Hi {{name1}} How {{name3}}
</div>
<script>
var app = angular.module('x', []);
app.controller("ctrl1" ,['$scope', '$rootScope',function($scope, $rootscope){
$scope.name2 = "Sir";
$rootscope.name3 = "are you!";
}]);
app.controller("ctrl2", function($scope){
$scope.name1 = "madam";
});
</script>
</body>
</html>
答案 1 :(得分:0)
请运行以下代码。问题解决了。你拼写错误的角度,你不需要将范围和rootScope放在控制器的ctrl1中的括号中。它也是rootScope而不是rootcope(检查大写S)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<body ng-app="x">
<div ng-controller="ctrl1">
Hello {{name2}}
</div>
<div ng-controller="ctrl2">
Hi {{name1}} How {{name3}}
</div>
<script>
var app= angular.module("x",[]);
app.controller("ctrl1" ,function($scope, $rootScope){
$scope.name2 = "Sir";
$rootScope.name3 = "are you!";
});
app.controller("ctrl2", function($scope){
$scope.name1 = "madam";
});
</script>
</body>
&#13;