我有以下控制器可以正常工作:
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;
}
});
}
};
我想使用AngularJS .scope。$ bind看看我是否可以消除'var that = this;'黑客攻击。但以下不起作用:
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;
}
}))();
}
};
我错过了什么?
答案 0 :(得分:2)
角度邮件上的Misko Hevery回复:
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。