【发布时间】:2018-08-04 05:04:13
【问题描述】:
我将在我的应用程序中使用 AngularJS 服务并让一切变得干净整洁。我为此目的关注了一些文章。但它似乎没有完成。对吗??
我没有看到任何错误或其他东西,但是当我设置 Alert(data);我会得到未定义的错误。我在这里错过的工作是什么?
我的 App.js
var app = angular.module('starter', ['ionic'])
我的服务.js
var serverPath = 'http://url/api/locations';
app.service('testService', function($http) {
this.getLocations = function() {
$http.get(serverPath).success(function(data) {
return data;
});
};
});
我的控制器.js
app.controller('LocationsController', function ($scope, testService) {
$scope.locations = testService.getLocations();
});
和我的用户界面
<div ng-controller="LocationsController">
<ul>
<li ng-repeat="location in locations">
{{ location.locationName }}
</li>
</ul>
</div>
【问题讨论】:
-
你在哪里跑
Alert(data)? -
@gtlambert 在我的 LocationsController 中。我删除了这一行
标签: javascript angularjs asp.net-web-api angularjs-service angular-promise