不确定这是否可行,但我似乎无法让它发挥作用。
基本前提
我想让用户单击表中的“编辑”按钮来打开一个模式,其中填充了单击的行的内容。
方法
我想在不使用 URL 参数的情况下执行此操作。填充表格的 API 响应已经提供了填充模态所需的一切,那么为什么要在 URL 中传递 :ID 来进行另一个 API 调用以再次获取必要的数据呢?
这是我的表格,用户可以单击该表格来启动该过程。
<tbody>
<tr ng-repeat="item in accounts">
<td><span class="label label-success">Active</span></td>
<td>{{item.login}}</td>
<td>{{item.full_name}}</td>
<td>{{item.member_since}}</td>
<td>Daily</td>
<td><a ng-click="showLinks(item)" class="btn btn-warning">LINKS</a></td>
<td><a ng-click="editAccount(item)" class="btn btn-primary">EDIT</a></td>
<td><a ng-click="deleteAccount(item)" class="btn btn-danger">DELETE</a></td>
</tr>
</tbody>
在我的 Controller 中,我有以下内容。
$scope.editAccount = function (item) {
console.log("Editing Account");
console.log(item);
$state.go('edit', item);
}
控制台正确打印出该对象。此时一切都很好。我想将此时选定的项目传递给 stateProvider,以便它可以填充 onEnter 函数中的模式。
请注意,我已经注释掉了“return {login: 'TEST'}”。如果我将其保留,模式将正确填充。
$stateProvider.state('edit', {
url: '/edit',
onEnter: function($stateParams, $state, $modal) {
console.log("Enter Edit");
$modal.open({
templateUrl: "views/account_edit.htm",
//params: ['login'],
resolve: {
item: function() {
console.log("Resolving Edit");
console.log($stateParams);
console.log($state.params);
console.log(this);
//return {login:"TEST"};
}
},
controller: ['$scope', 'item', function($scope, item) {
console.log("Controlling Edit");
$scope.item = item;
$scope.cancel = function() {
$scope.$close(true);
};
$scope.ok = function() {
$scope.$close(true);
};
}]
}).result.then(function(result) {
if (result) {
console.log("Acting on RESULT");
return $state.go("home");
}
});
}
});
我似乎无法开始工作的是如何从 $stateProvider 中访问 $stage.go 中传递的 ITEM。有人有主意吗?下面的文档表明我应该能够看到 $stateParams 中的数据,但它不在那里
转到(目标、参数、选项)
params(可选) – {object=} – 将发送到状态的参数映射,将填充 $stateParams。任何未指定的参数都将从当前定义的参数继承。例如,这允许进入共享父状态中指定的参数的同级状态。参数继承仅在共同祖先状态之间起作用,即转换为 sibling 将为您提供所有 parent 的参数,转换为 child 将为您提供所有当前参数,等等。
**更新
我发现实现这项工作的唯一方法是进行以下更新。
$scope.editAccount = function (item) {
console.log("Editing Account");
console.log(item);
$state.params.item = item;
//$state.go('edit', item);
$state.go('edit');
};
resolve: {
item: function() {
return $state.params.item;
}
},
最佳答案
要将参数传递给状态,您需要在状态的 url 选项中定义此参数。如果你不想,你可以在 params 选项中定义参数:
$stateProvider.state('edit', {
url: '/edit',
params: {
login: null,
full_name: null,
member_since: null
},
onEnter: function($stateParams, $state, $modal) {
// ...
}
});
关于AngularJS,将数据传递给 stateProvider OnEnter 函数以填充模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24963410/