国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

angular.js - angularjs 如何在controller中使用service
ringa_lee
ringa_lee 2017-05-15 16:50:58
0
3
824

Service代碼:

app.service('getTicketList', function($q, $http){
    this.data = {};
    this.getData = function(id){
        var deferred = $q.defer();
        var path='ticket.action?method:projectTickets';
        if(id)
            path+='&projectId='+id.toString();
        $http.get(path).then(function(d){
            //success
            this.data = d;
            deferred.resolve(d);
        },function(){
            //failure
            deferred.reject(d);
        });
    }
});

controller代碼:

app.controller('projectController', function($scope,getTicketList) {
    $scope.tickets=getTicketList.getData($scope.projectId).data.tickets;
});

controller的那句代碼有問題。我用getTicketList.data也獲取不到數(shù)據(jù),是{}。而且不知道怎么把參數(shù)傳進去。。。。

ringa_lee
ringa_lee

ringa_lee

全部回復(fù)(3)
巴扎黑

1.service里面用return代替this

javascriptapp.service('getTicketList', function ($q, $http) {
  var data = {};
  return {
    getData: function (id) {
      var deferred = $q.defer();
      var path = 'ticket.action?method:projectTickets';
      if (id)
        path += '&projectId=' + id.toString();
      var promise = $http.get(path).then(function (response) {
          return response;
      }, function (response) {
          return response
      });
      return promise;
    }
  }
});

2.調(diào)用的時候使用promise模式

javascriptgetTicketList.getData($scope.projectId).then(
  function (res) {
    $scope.tickets = res.tickets
  }, 
  function (rej) {
  // error handler
  }
);
漂亮男人

這種傳遞參數(shù)的方式是不對的,沒這么用過,這里有個項目,找到對應(yīng)的controller.service看看
https://github.com/LittleDouBi/community

過去多啦不再A夢

你應(yīng)該使用promise模式:

app.service('getTicketList', function ($q, $http) {
  this.data = {};
  this.getData = function (id) {
    var deferred = $q.defer();
    var path = 'ticket.action?method:projectTickets';
    if (id) {
      path += '&projectId=' + id.toString();
    }
    return $http.get(path).then(function (d) {
      this.data = d;
      return $q.when(d);
    }, function (d) {
      return $q.reject(d);
    });
  }
});

app.controller('projectController', function ($scope, getTicketList) {
  getTicketList.getData($scope.projectId).then(function (res) {
    $scope.tickets = res.tickets
  }, function (rej) {
    // error handler
  });
});
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板