我想從使用ui-router-extras
的JSON文件動態加載狀態。 JSON文件如下所示:AngularJS狀態加載
[
{
"name": "app",
"url": "/app",
"abstract": "true",
"resolve": "helper.resolveFor('fastclick', 'modernizr', 'icons')"
},
{
"name": "login",
"url": "/login",
"title": "User Login",
"templateUrl": "",
"redirectToLogin": "true"
},
{
"name": "dashboard",
"url": "/dashboard",
"title": "Dashboard",
"templateUrl": "helper.basepath('dashboard.html')",
"resolve": "helper.resolveFor('flot-chart', 'flot-chart-plugins', 'weather-icons')"
}
]
以下是路由配置文件:
(function() {
'use strict';
angular
.module('app.routes')
.config(routesConfig);
routesConfig.$inject = ['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider', '$stateProvider', '$futureStateProvider'];
function routesConfig($stateProvider, $locationProvider, $urlRouterProvider, helper, $sp, $fsp) {
$locationProvider.html5Mode(false);
var futureStateResolve = function($http) {
return $http.get("states.json").then(function (response) {
console.log(response.data);
angular.forEach(response.data, function (state) {
$sp.state(state);
})
})
}
$fsp.addResolve(futureStateResolve);
// defaults to dashboard
$urlRouterProvider.otherwise('/app/login');
}
})();
的/login
狀態工作正常,但其他人不工作,給出錯誤'invocables must be object'。我認爲這是因爲helper.basepath()
和helper.resolveFor()
函數不起作用,因爲它們是來自JSON的字符串。
我該怎麼辦?
作爲函數把它作爲2個屬性...函數名和參數 – charlietfl
不清楚實際 – user3036757
是我不好......只需要陣列用於解析'{「resolve」:「['flot-chart','flot-chart-plugins','weather-icons'] ...} ... ...然後傳遞數組並使用apply().. ''helper.resolveFor.apply(null,obj.resolve)' – charlietfl