我试图将状态动态添加到我的应用程序,并尝试使用ui-router。我尝试遵循此线程。
就我而言,已经有一些现存状态,我需要追加到该列表中,并从json读取动态状态
由于某种原因,当尝试用于deferIntercept()方法时,在$urlRouterProvider上出现注入器错误。就我而言,我使用的是angular1.3,而ui路由器的版本是0.2.10。我看到您可以协同创建状态。但是我们可以添加到已经静态配置的现有状态列表中吗
这是我的代码,对您有所帮助,
我的modules.json,
[{ "name": "applications1", "url": "^/templates/applications1", "parent": "authenticated", "abstract": false, "views": [{ "name": "", "templateUrl": "html/templates/basicLayout.html" }, { "name": "header@applications1", "templateUrl": "html/templates/header.html" }], { "name": "login", "url": "/login", "abstract": false, "views": [{ "name": "", "templateUrl": "html/admin/loginForm.html" }] }]
我的app.js
var $stateProviderRef = null; var $urlRouterProviderRef = null; var aModule = angular.module('App', [ 'ui.bootstrap','ui.router' ]); adminModule.run(['$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; }]) adminModule.run(['$q', '$rootScope','$http', '$urlRouter', function ($q, $rootScope, $http, $urlRouter) { $http .get("modules.json") .success(function(data) { angular.forEach(data, function (value, key) { var state = { "url": value.url, "parent" : value.parent, "abstract": value.abstract, "views": {} }; angular.forEach(value.views, function (view) { state.views[view.name] = { templateUrl : view.templateUrl, }; }); $stateProviderRef.state(value.name, state); }); // Configures $urlRouter's listener *after* your custom listener $urlRouter.sync(); $urlRouter.listen(); }); }]); aModule.config(['$locationProvider', '$stateProvider', '$urlRouterProvider', '$httpProvider', function ($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider) { // XSRF token naming $httpProvider.defaults.xsrfHeaderName = 'x-dt-csrf-header'; $httpProvider.defaults.xsrfCookieName = 'X-CSRF-TOKEN'; $httpProvider.interceptors.push('httpInterceptor'); $stateProvider .state('login', { url: '/login', templateUrl: 'html/XXX/loginForm.html', controller: 'AController' }) .state('sAgree', { url: '/serviceAgreement', templateUrl: 'html/xxx/s.html', controller: 'SController' }); $urlRouterProvider.deferIntercept(); $urlRouterProvider.otherwise('/login'); $locationProvider.html5Mode({enabled: false}); $stateProviderRef = $stateProvider; $urlRouterProviderRef = $urlRouterProvider; }]);
上面有所有摘录,其中有一个工作正常的插件。
如果我们要添加一些尚不存在的状态,则应检查 $state.get('stateName')
$state.get('stateName')
$http .get("modules.json") .success(function(data) { angular.forEach(data, function(value, key) { // here we ask if there is a state with the same name var getExistingState = $state.get(value.name) // no need to continue, there is state (e.g. login) already if(getExistingState !== null){ return; } var state = { "url": value.url, "parent": value.parent, "abstract": value.abstract, "views": {} }; angular.forEach(value.views, function(view) { state.views[view.name] = { templateUrl: view.templateUrl, }; }); $stateProviderRef.state(value.name, state); }); // Configures $urlRouter's listener *after* your custom listener $urlRouter.sync(); $urlRouter.listen(); });
检查这里是否有效