使用美元的范围.$watch在控制器中使用' this '作用域时
Using $scope.$watch when using `this` scope in controller
在我的Angular应用中有一个控制器:
(function (angular) {
function MyController() {
this.name = 'Dave';
// I want to have code like this:
/*
$scope.$watch('name', function (newValue, oldValue) {
console.log(oldValue, "changed to", newValue);
});
*/
}
window.myApp = angular.module('myApp', [])
.controller('MyController', [MyController]);
})(angular);
当附加值到MyController
原型时,是否有方法使用$scope.$watch
的特征?
我确实注意到,在我的代码中,如果我添加类似ng-controller="MyController as myCtrl"
的东西,并将我的$scope.$watch
语句更改为$scope.$watch('myCtrl.name', ...)
,它将在我添加$scope
依赖项后工作,但这感觉就像将我的控制器绑定到我的视图,这感觉是错误的。
编辑
试图澄清我的问题。我的HTML是这样的:
<div ng-app="myApp">
<div ng-controller="MyController as myCtrl">
<input type="text" ng-model="myCtrl.name" />
<p>{{myCtrl.helloMessage}}</p>
</div>
</div>
我的控制器是这样的:
angular.module('myApp', [])
.controller('MyController', ['$scope', function ($scope) {
this.name = 'World';
this.helloMessage = "Hello, " + this.name;
var self = this;
$scope.$watch('myCtrl.name', function () {
self.helloMessage = "Hello, " + self.name;
});
}]);
这目前有效,但正如您所看到的,在$watch
调用中,我必须从视图中通过controllerAs
名称引用我的控制器,这是不理想的。
我已经设置了一个关于Plunkr的例子
$watch
在每个$digest循环上求值的表达式。返回值的更改会触发对侦听器的调用。Watch表达式可以是一个字符串或函数。
- 字符串:计算为表达式
- 函数(作用域):以当前作用域作为参数调用。
angular.module('app', []).controller('MainCtrl', function($scope) {
this.name = 'World'
this.helloMsg = ''
$scope.$watch(function() {
return this.name
}.bind(this), function(newName) {
this.helloMsg = "Hello, " + newName
}.bind(this))
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller='MainCtrl as ctrl'>
<input type='text' ng-model='ctrl.name' />
{{ ctrl.helloMsg }}
</div>
</div>
您可以通过在手表中使用angular.bind
来避免绑定到视图,即
$scope.$watch(angular.bind(this, function () {
self.helloMessage = "Hello, " + self.name;
}));
https://docs.angularjs.org/api/ng/function/angular.bind 我可能错了,但我相信$作用域是由angular自动注入的。
下面是一个声明控制器的例子:
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.greeting = 'Hola!';
}]);
注意$scope依赖是如何声明'$scope'
和注入function($scope)
的
换句话说,你的应该是这样的吗?
function MyController($scope) {}
window.myApp = angular.module('myApp', [])
.controller('MyController', ['$scope', MyController($scope)]);
编辑:我现在明白了。我从来没有需要使用"controller as",但为什么不这样做呢?
<div ng-app="myApp">
<div ng-controller="MyController">
<input type="text" ng-model="name" />
<p>{{helloMessage}}</p>
</div>
</div>
angular.module('myApp', [])
.controller('MyController', ['$scope', function ($scope) {
this.name = 'World';
this.helloMessage = "Hello, " + this.name;
var self = this;
$scope.$watch('name', function () {
self.helloMessage = "Hello, " + self.name;
});
}])
相关文章:
- 在函数中获取正确的作用域(不使用that=this)
- “this”和内部作用域命名函数的打字稿问题
- 通过引用调用原型函数时,类会丢失“this”作用域
- Node.js:在模块作用域中使用“this”运算符的上下文是什么
- jQuery 样板 - 失去对“this”作用域的访问权限
- 使用this from函数在作用域上获取/设置角度变量
- JavaScript/jQuery类中的变量作用域(this)
- Javascript变量作用域问题-错误:类型错误:this.graphics未定义
- 为什么在代码示例中' this '不是指向js全局作用域
- 如何向前'this'javascript中函数调用的作用域
- 使用美元的范围.$watch在控制器中使用' this '作用域时
- 访问被点击的元素和"this"类引用在同一作用域中
- 在Javascript中,“this.something”是否在作用域链中查找“something”
- 改变“this"主干集合的作用域
- 成员可见性和'this'javascript对象中的作用域
- Javascript作用域基础:.bind或self =this
- 我怎么能得到自己的范围在定义模块在require.js?作用域是'this'我希望它是模块本身
- Javascript作用域和this.Variable
- Typescript中this的子作用域问题
- TypeScript:那么"this"作用域