2011-07-11 50 views
3

我有以下控制器的正常工作:。

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 $綁定,看看我是否能消除「無功即=這個;'黑客攻擊。但以下不起作用:

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

請不要多次提問相同的問題。謝謝。 – Kev

+0

@Graham在這個問題中的用戶詢問'angular.scope'作爲編程構造,而不是作爲庫的名稱。標籤旨在用於可搜索性。 –

回答

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建議。