如何使用angular.scope。$绑定 [英] How to use angular.scope.$bind
本文介绍了如何使用angular.scope。$绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下控制器的正常工作:
I have the following controller which works fine:
function Controller() {}
Controller.prototype = {
getResult: function(project) {
var that = this;
jQuery.ajax({
async: false,
url: "/my-service/call?project=" + project,
dataType: "json",
success: function(data) {
that.result = data;
}
});
}
};
我想使用angular.scope $绑定,看看我是否能消除。VAR是这个=;'黑客攻击。但下面不工作:
I'd like to use angular.scope.$bind to see if I could eliminate the 'var that = this;' hack. But the following doesn't work:
function Controller() {}
Controller.prototype = {
getResult: function(project) {
angular.scope.$bind(jQuery.ajax({
async: false,
url: "/my-service/call?project=" + project,
dataType: "json",
success: function(data) {
this.result = data;
}
}))();
}
};
我是什么失踪?
推荐答案
MISKO Hevery的角度邮寄与回应:
Misko Hevery on the angular mailing responded with:
Controller.prototype = {
getStuff: function(project) {
jQuery.ajax({
async: false,
url: "/service/get-stuff",
dataType: "json",
success: angular.bind(this, function(data) {
this.stuff = data;
})
});
}
};
他还使用angular.service。$ XHR而不是jQuery.ajax建议。
He also suggested using angular.service.$xhr instead of jQuery.ajax.
这篇关于如何使用angular.scope。$绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文