我创建了单独的服务和控制器并将它们注入到 htapp模块
我希望将url中的消息显示在html页面中,但是我从js脚本获取空值并且它在html页面中显示为null
angular.module('htapp', []).service('testService',testService).controller('TestCtrl', TestCtrl);
function TestCtrl(testService, $log) {
self.getMessage = function() {
testService.get()
.then(function(message) {
$log.info(message);
self.message = message;
})
}
}
//serivce method
function testService($http, $log) {
this.get = function() {
return $http({
url: 'test-routes.herokuapp.com/test/hello',
method: 'get'
})
.then(function successCallback(res) {
return res.data.message;
}, function errorCallback(res) {
return res.data;
})
$log.info(res);
}
}

<!--HTML CODE-->
<div ng-app="htapp">
<div ng-controller="TestCtrl as test">{{test.message}}
</div>
</div>
&#13;
答案 0 :(得分:0)
angular.module('htapp', []).service('testService',testService).controller('TestCtrl', TestCtrl);
function TestCtrl(testService, $log) {
var test = this;
test.getMessage = function() {
testService.get()
.then(function(message) {
$log.info(message);
self.message = message;
})
}
}
//serivce method
function testService($http, $log) {
this.get = function() {
return $http({
url: 'test-routes.herokuapp.com/test/hello',
method: 'get'
})
.then(function successCallback(res) {
return res.data.message;
}, function errorCallback(res) {
return res.data;
})
$log.info(res);
}
}
<强> HTML 强>
<div ng-app="htapp">
<div ng-controller="TestCtrl as test">{{test.message}}
</div>
</div>
阅读更多信息here