我在列出ng-repeat指令中的一些元素时遇到了麻烦,而我附加了ui-sref属性。尝试在控制器内部使用ng-click和$ state.go但没有成功。
顺便说一句,我正在将params传递给那些ui-sref指令,当我查看控制台时,这些参数是好的,但是API路径是错误的。
已经搜索了几个小时,但找不到答案。
这是我的代码:
<h4 class="list-group-item-heading">{{tasklist.name}}</h4>
<div class="table-responsive connectList" ui-sortable="sortableOptions" ng-repeat="task in tasklist.tasks">
<span class="btn btn-primary" ui-sref="project-detail.taskedit({id:task.id})"> Edit </span>
<span class="btn btn-primary" ui-sref="project-detail.taskdelete({id:task.id})"> Remove </span>
</div>
<div class="row">
<button class="btn btn-xs btn-new-task" ui-sref="project-detail.tasknew({id:tasklist.id})">
New Task
</button>
</div>
</div>
我正在打电话的州:
(function() {
'use strict';
angular
.module('velettaApp')
.config(stateConfig);
stateConfig.$inject = ['$stateProvider'];
function stateConfig($stateProvider) {
$stateProvider
.state('project', {
parent: 'entity',
url: '/project',
data: {
authorities: ['ROLE_USER'],
pageTitle: 'velettaApp.project.home.title'
},
views: {
'content@': {
templateUrl: 'app/entities/project/projects.html',
controller: 'ProjectController',
controllerAs: 'vm'
}
},
resolve: {
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('project');
$translatePartialLoader.addPart('global');
return $translate.refresh();
}]
}
})
.state('project-detail', {
parent: 'entity',
url: '/project/{id}',
data: {
authorities: ['ROLE_USER'],
pageTitle: 'velettaApp.project.detail.title'
},
views: {
'content@': {
templateUrl: 'app/entities/project/project-detail.html',
controller: 'ProjectDetailController',
controllerAs: 'vm'
}
},
resolve: {
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('project');
return $translate.refresh();
}],
entity: ['$stateParams', 'Project', function($stateParams, Project) {
return Project.get({id : $stateParams.id}).$promise;
}],
loadPlugin: function ($ocLazyLoad) {
return $ocLazyLoad.load([
{
name: 'ui.sortable',
files: ['app/plugins/ui-sortable/sortable.js']
}
]);
}
}
})
.state('project-detail.tasklist', {
parent: 'project-detail',
url: '/task-list/new',
data: {
authorities: ['ROLE_USER']
},
onEnter: ['$stateParams', '$state', '$uibModal', 'Project',function ($stateParams, $state, $uibModal, Project) {
$uibModal.open({
templateUrl: 'app/entities/task-list/task-list-dialog.html',
controller: 'TaskListDialogController',
controllerAs: 'vm',
backdrop: 'static',
size: 'lg',
resolve: {
entity: function () {
return {
name: null,
dateCreated: moment().utc().format().slice(0, -1)+'.000Z',
isPrivate: null,
id: null,
project: Project.get({id : $stateParams.id}).$promise
};
},
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('taskList');
return $translate.refresh();
}]
}
}).result.then(function () {
$state.go('project-detail', null, {reload: true});
}, function () {
$state.go('project-detail');
});
}]
})
.state('project-detail.tasknew', {
parent: 'project-detail',
url: '/task/new',
data: {
authorities: ['ROLE_USER']
},
onEnter: ['$stateParams', '$state', '$uibModal','TaskList', function ($stateParams, $state, $uibModal, TaskList) {
$uibModal.open({
templateUrl: 'app/entities/task/task-dialog.html',
controller: 'TaskDialogController',
controllerAs: 'vm',
backdrop: 'static',
size: 'lg',
resolve: {
entity: function () {
return {
creatorId: null,
title: null,
type: null,
dateCreated: moment().utc().format().slice(0, -1)+'.000Z',
dateDue: null,
taskStatus: null,
priority: null,
isCompleted: null,
id: null,
taskList: ['$stateParams', 'TaskList', function($stateParams, TaskList) {
return TaskList.get({id : $stateParams.id}).$promise;
}]
};
},
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('task');
return $translate.refresh();
}]
}
}).result.then(function () {
$state.go('project-detail', null, {reload: true});
}, function () {
$state.go('project-detail');
});
}]
})
.state('project-detail.taskedit', {
parent: 'project-detail',
url: '/task/{id}/edit',
data: {
authorities: ['ROLE_USER']
},
onEnter: ['$stateParams', '$state', '$uibModal','Task', function ($stateParams, $state, $uibModal, Task) {
$uibModal.open({
templateUrl: 'app/entities/task/task-dialog.html',
controller: 'TaskDialogController',
controllerAs: 'vm',
backdrop: 'static',
size: 'lg',
resolve: {
entity: ['Task', function(Task) {
return Task.get({id : $stateParams.id}).$promise;
}],
translatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate, $translatePartialLoader) {
$translatePartialLoader.addPart('task');
return $translate.refresh();
}]
}
}).result.then(function () {
$state.go('project-detail', null, {reload: true});
}, function () {
$state.go('project-detail');
});
}]
})
.state('project-detail.taskdelete', {
parent: 'project-detail',
url: '/task/{id}/delete',
data: {
authorities: ['ROLE_USER']
},
onEnter: ['$stateParams', '$state', '$uibModal','Task', function ($stateParams, $state, $uibModal,Task) {
$uibModal.open({
templateUrl: 'app/entities/task/task-delete-dialog.html',
controller: 'TaskDeleteController',
controllerAs: 'vm',
size: 'md',
resolve: {
entity: ['Task', function (Task) {
return Task.get({id: $stateParams.id}).$promise;
}]
}
}).result.then(function () {
$state.go('project-detail', null, {reload: true});
}, function () {
$state.go('^');
});
}]
})
非常感谢!
答案 0 :(得分:1)
查看project-detail.taskedit
的网址:
url: '/task/{id}/edit',
现在,它的父状态project-detail
, url :
url: '/project/{id}', // Note the same {id} as my child 'project-detail.taskedit' state
在两种状态下,您都有相同的参数名称 id ,因此您可以将project-detail.taskedit
的网址更改为
url: '/task/{taskId}/edit', // My new parameter name won't get ui-router confuse my params and my parent parameters! :)
然后ui-sref
应该是:
ui-sref="project-detail.taskdelete({id:tasklist.id, taskId:task.id})"
这将使ui-router
能够使用正确的值附加参数。
请注意,您还应该通过将引用onEnter
更改为project-detail.taskdelete
来更改$stateParams.id
的{{1}}设置(以及应该引用的任何其他位置)新的 taskId 参数)。
您也应该在$stateParams.taskId
状态下进行相同的更改(将参数替换为'project-detail.taskedit'
,依此类推......)