How to use Angular service in place of Ajax url property

时间:2015-07-29 00:41:18

标签: ajax angularjs-service jeasyui

Here is code:

NoPermissions

loader:function(param,success,error){ $.ajax({ //url: 'http://localhost/mvcController?assetid=1&dataformat=attribute', url: assetsMVCService.execute("mvcController", { assetId: 1, dataFormat: 'attribute' }), dataType: 'text', type: 'post', success: function (data) { alert('ok'); }, error: function () { error.apply(this, arguments); } }); is my wrapper for Angular service. Unfortunately, a product that I am implementing forces me to use AJAX call to get data. Normally, I would use services to get data and then scope for data binding. Is it possible to use a service assigned to the url property in the code above? Interesting enough, I am hitting server with the code above. But something gets wrong on the server.

Thanks

1 个答案:

答案 0 :(得分:0)

是。你可以这样做:

app.service('MVC', function($http) {
  var root = 'http://localhost/mvcController';
  var queryParams = '?assetid=1&dataformat=attribute';

  this.get = function(num) {
    return $http.get(root + '/' + num + queryParams);
  };

  // or if you want to pass the query params in
  this.execute = function(assetId, dataFormat) {
    return $http.get(root + '?assetId=' + assetId + '&dataFormat=' + dataFormat;
  };

  // other routes
});

请注意,当您使用Angular时,可以而且应该使用$http代替$.ajax。它与$.ajax几乎完全相同,只不过它与Angular很好用。