Javascript AngularJS的范围界定问题
我是一个有点javascript/角度的新手,在理解作用域如何工作时遇到了一些困难。由于范围问题,我有以下代码无法运行: 角度:Javascript AngularJS的范围界定问题,javascript,angularjs,scope,Javascript,Angularjs,Scope,我是一个有点javascript/角度的新手,在理解作用域如何工作时遇到了一些困难。由于范围问题,我有以下代码无法运行: 角度: this.myFxn = function() { var x = this.myModel; //this.myModel is set by an ng-model in the html myService.myServiceFxn(x.Id) .then(function (response) { th
this.myFxn = function() {
var x = this.myModel; //this.myModel is set by an ng-model in the html
myService.myServiceFxn(x.Id)
.then(function (response) {
this.myModel = "";
});
};
我相信问题在于我在.then()中引用了这个.myModel。如何在没有任何范围问题的情况下正确引用此变量
谢谢你能给予的任何帮助!:) 只需在变量中保留对控制器的引用,如示例中的
self
this.myFxn = function() {
var self = this;
myService.myServiceFxn(self.myModel.Id)
.then(function (response) {
self.myModel = "";
});
};
只需将对控制器的引用保留在变量中,如示例中的
self
this.myFxn = function() {
var self = this;
myService.myServiceFxn(self.myModel.Id)
.then(function (response) {
self.myModel = "";
});
};