【发布时间】:2014-12-04 03:51:26
【问题描述】:
我有 ui-router、resolve、factory 和 $http.get 调用的错误(或者可能是错误的用法?)。
这是配置部分中代码的 sn-p:
$stateProvider
.state('index', {
url: '/',
views: {
'': {
templateUrl: './views/layout.html',
controller: 'MyAppCtrl'
},
'app-navbar@index': {
templateUrl: './views/app-navbar.html'
},
'app-accordion@index': {
templateUrl: './views/app-accordion.html',
controller: 'AppController',
resolve: {
appPromiseObj: function (AppFactory) {
return AppFactory.getApps();
}
}
},
...
并拥有以下 AppFactory
myApp.factory('AppFactory', function ($http) {
var appFac = {
apps: []
};
appFac.getApps = function () {
promiseObj = $http
.get('http://localhost:4567/applications')
.success(function (data) {
console.log("success calling http");
angular.copy(data, appFac.apps);
});
return promiseObj;
};
return appFac;
});
但是当我运行应用程序时,'success' 回调中的 console.log 消息永远不会被执行。浏览器控制台日志显示 http 调用执行正常,代码为 200。我假设这意味着 angular 认为它已经失败或者我应该做其他事情吗?
我什至尝试返回 $q 承诺对象(正如其他一些相关的堆栈溢出线程中所建议的那样)但没有成功。在工厂代码中,如果我使用测试数据(即没有 HTTP 调用),即使我没有返回 promise 对象,一切正常。关于问题可能出在哪里的任何指针?感谢任何可以帮助我调试的指针...
【问题讨论】:
标签: javascript angularjs angular-ui-router