我了解 UI-router 的配置设置,它工作正常。现在我正在尝试将以下配置移动到指令中。这样,js文件中的代码长度就会减少。可能是,这是糟糕的设计,但我想实现这一点:)
当前配置(根据 UI 路由器设计)
//Router Configuration
angular.module('myApp', ['ui.router']).config(function($stateProvider) {
$stateProvider.state('addCustomer', {
url: "/addCustomer",
templateUrl: 'views/customer/add_customer.html',
controller: 'addCustomerController'
});
...No of configuration, list is very big...
});
//In Template
<a ui-sref="addCustomer">Add Customer</a>
我想要改变的
//Router Configuration
angular.module('myApp', ['ui.router']).config(function($stateProvider) {
});
//In Template
<a ui-sref="addCustomer" router-info="{'url':'/addCustomer', 'templateUrl':'views/customer/add_customer.html', 'controller':'addCustomerController'}">Add Customer</a>
//Directive- Dynamic routing
angular.module('myApp').directive('routerInfo', function(){
var directive = {};
directive.restrict = 'A';
directive.compile = function(element, attributes) {
var linkFunction = function($scope, element, attributes) {
//Here I understand, I can not inject stateprovider. So kindly suggest any other way to do this
//$stateProvider.state(attributes.uiSref, attributes.routerInfo);
}
return linkFunction;
}
return directive;
});
如何从指令中添加 UI 路由器配置?有可以设置的API吗?或任何其他更好的方法来处理这个......我的目的是减少配置块中的代码。