Rumah > Soal Jawab > teks badan
Kod perkhidmatan:
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);
});
}
});
kod pengawal:
app.controller('projectController', function($scope,getTicketList) {
$scope.tickets=getTicketList.getData($scope.projectId).data.tickets;
});
Terdapat masalah dengan kod pengawal. Saya tidak boleh mendapatkan data menggunakan getTicketList.data, ia {}. Dan saya tidak tahu bagaimana untuk menghantar parameter. . . .
巴扎黑2017-05-15 16:52:58
javascript
app.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; } } });
javascript
getTicketList.getData($scope.projectId).then( function (res) { $scope.tickets = res.tickets }, function (rej) { // error handler } );
漂亮男人2017-05-15 16:52:58
Cara menghantar parameter ini adalah salah Saya tidak pernah menggunakannya seperti ini. Terdapat projek di sini. 🎜>
过去多啦不再A梦2017-05-15 16:52:58
Anda harus menggunakan mod janji:
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
});
});