我正在尝试将控制器范围变量设置为数据,但没有运气。控制器中的console.log显示未定义。感谢帮助!
我的角度服务有以下代码 -
<title>
<c:choose>
<c:when test="${requestScope['com.escenic.context']=='art'}">
<c:out value="${article.title}" escapeXml="true"/> -
</c:when>
<c:when test="${section.uniqueName != 'homepage’}”>
<c:out value="${section.name}" escapeXml="true"/> -
</c:when>
</c:choose>
<c:choose>
<c:when test=“${wesite.name eq ‘pg’}”>
pg website name
</c:when>
<c:when test="${wesite.name eq ‘pk’}”>
pk website name
</c:when>
<c:when test="${wesite.name eq ‘ls’}”>
ls website name
</c:when>
</c:choose>
</title>
我在控制器中引用它如下 -
service('VyrtEventService', ['$http', function($http) {
var events = [];
this.getArtistEvents = function(artistId) {
var url = '/api/users/' + artistId + '/';
var promise = $http.get(url).success(function(data, status, headers, config) {
events = data.artist.events;
console.log(events);
return events;
}).catch(function(error) {
status = 'Unable to load artist data: ' + error.message;
console.log(status);
});
return promise;
};
}]);
答案 0 :(得分:0)
您应该在控制器中设置$scope.events = data
,因为您的承诺在结算时已经返回data.artist.events
答案 1 :(得分:0)
从控制器的任何位置将范围传递给服务。确保注入服务。
controllersModule.controller('MyCtrl', function($scope, $filter, $http, $compile, ngTableParams, **FactoryYouwant**)
{
**FactoryYouwant**.getdata($scope.**scopeYoutwantTopass**).then (function(responseData){
var ResponseFromServer =responseData.data;
}
in service
controllersModule.factory('**FactoryYouwant**, function($http) {
var responseData = null;
return {
getdata : function(**data**){ (you dont have to use $)
responseData = $http.post or whatever actually gets you data;
return responseData;
}
};
});
我希望这可以帮助您在控制器的任何位置调用从服务获取数据。