Angularjs 用户界面路由器。从对象/函数获取状态名
我想知道,有没有办法用对象或函数引用视图中的状态Angularjs 用户界面路由器。从对象/函数获取状态名,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我想知道,有没有办法用对象或函数引用视图中的状态 只是为了将视图与状态定义解耦。例如,如果我更改了州名称,我不必在视图中的任何地方都更改它。可以在此处找到一种解决方案,如下所述,作为一种工作模式 在本例中,我们将为某些实体(如员工)定义状态,如: 列表查看和 详细信息查看 让我们使用一些变量entityName来扮演解耦命名的角色: var entityName = "employee"; $stateProvider .state(entityName, { url:
只是为了将视图与状态定义解耦。例如,如果我更改了州名称,我不必在视图中的任何地方都更改它。可以在此处找到一种解决方案,如下所述,作为一种工作模式 在本例中,我们将为某些实体(如员工)定义状态,如:
entityName
来扮演解耦命名的角色:
var entityName = "employee";
$stateProvider
.state(entityName, {
url: '/' + entityName,
views: {
...
}})
.state(entityName + '.detail', {
url: '/{{Id}}',
views: {
...
}});
从列表到详细视图的导航(如我们所见,没有使用明确的“员工”名称):
就这样。它可能更复杂。。。可以找到并运行完整的示例代码(以下作为states definition附上)
.config(['$stateProvider',
函数($stateProvider){
var entityName=“employee”;
$stateProvider
.state(entityName{
url:“/”+entityName,
观点:{
正文:{
模板:“”+
“列表视图”+
“”+
“- {{item.Name}”+
“
”+
“详细视图”+
' ' +
'',
控制器:['$scope','$state',
函数($scope,$state){
$scope.items=[{Name:“Abc”,Id:0},{Name:“Def”,Id:1}];
$scope.detailLink=函数(项){
var currentState=$state.current.name;
返回currentState+'.detail({Id:'+item.Id+'}');
};
}],
}
}})
.state(entityName+“.detail”{
url:“/{Id}}”,
观点:{
详情:{
模板:“”+
“{item.Name}”+
“如果有帮助的话,那就太好了;)ui路由器是个不错的选择!
<a ui-sref="{{detailLink(item)}}" >{{item.Name}}</a>
controller:['$scope','$state',
function ( $scope , $state){
$scope.detailLink = function (item){
// here we get the employee in run-time
var currentState = $state.current.name;
var sref = currentState + '.detail({Id:' + item.Id + '})';
return sref;
};
}],
.config(['$stateProvider',
function( $stateProvider) {
var entityName = "employee";
$stateProvider
.state(entityName, {
url: '/' + entityName,
views: {
body: {
template: '<div>' +
' <h2>List View</h2> ' +
' <ul ng-repeat="item in items"> ' +
' <li><a ui-sref="{{detailLink(item)}}" >{{item.Name}}</a>' +
' </li></ul>' +
' <h2>Detail View</h2> ' +
' <div ui-view="detail"></div>' +
'</div>',
controller:['$scope','$state',
function ( $scope , $state){
$scope.items = [{Name : "Abc", Id : 0}, {Name : "Def", Id : 1}];
$scope.detailLink = function (item){
var currentState = $state.current.name;
return currentState + '.detail({Id:' + item.Id + '})';
};
}],
}
}})
.state(entityName + '.detail', {
url: '/{{Id}}',
views: {
detail: {
template: '<div>' +
' <label>{{item.Name}} ' +
' <input ng-model="item.Name"}}" type="text" />' +
' <div>current state name: <i>{{state.name}}<i></div> ' +
'</div>',
controller:['$scope','$stateParams','$state',
function ( $scope , $stateParams , $state){
$scope.state = $state.current
$scope.item = $scope.items[$stateParams.Id];
}],
}
}});
}])