admin管理员组文章数量:1279124
In AngularJS they have $scope.$apply()
method to update the UI when there's a model change that is not done through normal AngularJS means.
In the more recent tutorials they remend using the <controller> as <object>
style of instantiating the objects and use this
as the scope from their example
.controller('TodoListController', function() {
var todoList = this;
However todoList.$apply()
does not appear to work. Am I forced to use $scope.$apply()
for this?
In AngularJS they have $scope.$apply()
method to update the UI when there's a model change that is not done through normal AngularJS means.
In the more recent tutorials they remend using the <controller> as <object>
style of instantiating the objects and use this
as the scope from their example
.controller('TodoListController', function() {
var todoList = this;
However todoList.$apply()
does not appear to work. Am I forced to use $scope.$apply()
for this?
4 Answers
Reset to default 7Yes you have to use $scope.$apply(), but that's not a bad thing.
I had this same exact dilemma after reading that one should use controllerAs syntax. I even asked this question a few months later In an isolate scope directive is there any difference between defining variables on scope and defining variables on the controller?
The answer, after thinking about this for a while, is that controllerAs syntax doesn't mean an aversion to $scope
, but a design pattern to prevent global state from being stored in $scope
because that's when you start nesting scopes, which leads to a lot of problems.
$scope
isn't an evil thing. It just lets you screw yourself over, but if you need to use it you shouldn't stop yourself from doing so.
Think you mean the "controller as" syntax, would be good to update the question title. You can still inject $scope and use it for registering watches or whatever, typically you aren't calling $apply within a controller though, typically it's done in a directive in response to some event that changes the model and needs to trigger Angular to refresh.
The solution to this is pass this as bind parameter using either ES5 bind or angular.bind function
Eexmaple:
myApp.controller("myController",['$scope',function(s){
this.name="val1";
console.log("1: "+this.name);
setTimeout(function(){
console.log("2: "+this.name);
s.$apply(function(){
this.name="val2";
}.bind(this));
console.log("3: "+this.name);
}.bind(this));
}]);
Directly in the controller this
refers to the controller
.controller('TodoListController', function() {
var todoList = this;
Here todoList is the controller.
However in a method defined on the scope, this
refers to the scope. So
.controller('TodoListController', function($scope) {
$scope.myFunction = function() {
var todoList = this;
....
would have todoList pointing to the scope and you can do todoList.$apply()
本文标签: javascriptHow to do scopeapply() with the new controller as syntaxStack Overflow
版权声明:本文标题:javascript - How to do $scope.$apply() with the new `controller as` syntax? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741208195a2358572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论