我遇到GET请求后$ scope中没有变量的问题
路线提供者
when('/vote/:surveyId', { templateUrl: 'partials/polllist.html', controller: PollListCtrl }).
我有一个service.js会跟随代码
factory('Vote', function($resource) {
return $resource('vote/:surveyId', {}, {
// Use this method for getting a list of polls
vote : {
method : 'POST',
params : {
surveyId : 'vote'
},
isArray : true
},
getVote : {
method : 'GET',
isArray : true
}
})
})
我在PollListCtrl
中有以下代码V
ote.vote(voteObj, function(p, resp) {
console.log("outside --- NNNNOOOOO ERRRORRRRR"+$location);
if(!p.error) {
// If there is no error, redirect to the main view
console.log("NNNNOOOOO ERRRORRRRR"+resp);
$scope.vote_data = resp;
$scope.voted = Vote.getVote({
surveyId : surveyId
});
//$location.path('surveys');
//$location.path();
} else {
alert('Could not create survey');
}
});
你能帮我解决这个问题..
答案 0 :(得分:0)
我相信如果你想以这种方式在GET资源中指定参数,你必须使用@来执行映射。另外,您告诉资源参数使用“投票”变量,但是当您尝试调用端点时,您将其改为“surveyId”。我保留了你的定义结构并更改了你的控制器代码以匹配只是为了保持变化清晰。
另外,你没有发布所有的控制器代码,所以我不确定究竟是哪个surveyId(你试图传入路径的那个)来自哪里;我假设你在某处宣布了它。但如果那应该是一个范围变量,你会想要用$scope.
factory('Vote', function($resource) {
return $resource('vote/:surveyId', {}, {
vote : {
method : 'POST',
params : {
surveyId : '@vote'//<= Changed
},
isArray : true
},
getVote : {
method : 'GET',
isArray : true
}
})
})
Vote.vote(voteObj, function(p, resp) {
if(!p.error) {
$scope.vote_data = resp;
$scope.voted = Vote.getVote({vote : surveyId}); //<=Changed
} else {
alert('Could not create survey');
}
});
试试看,如果你还有问题,请告诉我。