在这本 ng-book JSBin中,确实由于原型继承而$scope.$watch()
解析。$rootScope.$watch()
我们可以$rootScope
在控制器内部显式注入,以便与控制器内部$scope
相同$rootScope
,而无需通过原型继承吗?
在此处复制代码以供参考:
// open this example and type person.name into the test field
angular.module('myApp', [])
.controller('MyController',
['$scope', '$parse', function($scope, $parse) {
$scope.person = {
name: "Ari Lerner"
};
$scope.$watch('expr', function(newVal, oldVal, scope) {
if (newVal !== oldVal) {
// Let's set up our parseFun with the expression
var parseFun = $parse(newVal);
// Get the value of the parsed expression, set it on the scope for output
scope.parsedExpr = parseFun(scope);
}
});
}]);