【发布时间】:2015-01-22 08:05:18
【问题描述】:
我对 AngilarJS 很陌生。我正在尝试用 angularJS 编写服务。
<script>
var module = angular.module("myapp", []);
module.service('BrandService', function ($http) {
var brands = [];
this.getBrands = function()
{
return $http.get('http://admin.localhost/cgi-bin/brand.pl')
.then(function(response)
{
brands = response.brands;
alert (brands);
});
}
//simply returns the brands list
this.list = function ()
{
return brands;
}
});
module.controller("brandsController", function($scope, BrandService) {
$scope.brandlist = BrandService.list();
alert ($scope.brandlist);
});
</script>
声明“警报(品牌);”没有被调用。这段代码有什么问题。 m 在实现中是否遗漏了任何东西?
【问题讨论】:
-
警报为空还是不警报?
-
服务中不提示,控制器中提示为空。
-
打开调试控制台,告诉我们错误信息是什么。
-
您的控制器是否在某处被调用? (例如在 ng-controller 指令中)
-
您可以尝试将 $window 注入服务并调用 $window.alert 而不是 alert 吗?
标签: angularjs