gpt4 book ai didi

angularjs - 使用 Angular UI 路由和 UI 模态在 AngularJS 中创建有状态的模态

转载 作者:行者123 更新时间:2023-12-03 08:13:25 24 4
gpt4 key购买 nike

我正在尝试使用 angular ui route 和 angular ui modal 制作一个有状态的模态。

假设我想填写一个包含 4 页的注册表单。所有这些页面都在模态中。

我能够在状态更改时打开模态。但在那之后,我想根据状态打开模态的每一页。

    $stateProvider.state('signup', {
url: '/signup',
template: '<ui-view />',
abstract: true
})
.state('signup.modal', {
url: '',
resolve: {
modalInstance: function($modal) {
return $modal.open({
template: '<div ui-view="modal"></div>',
controller: 'signUp',
windowClass: 'signup-edit-modal',
backdrop: 'static'
})
}
},
onEnter: ['modalInstance', '$timeout', '$state',function(modalInstance,$timeout, $state) {
modalInstance.opened.then(function(status){
if(status){
$timeout(function(){
// changing color of background / backdrop of modal for signup
angular.element('.reveal-modal-bg').css('backgroundColor','rgba(255, 255, 255, .9)');
})
}
})
$state.go('signup.welcome')
}],
onExit: function(modalInstance) {
modalInstance.close('dismiss');
angular.element('.reveal-modal-bg').css('backgroundColor','rgba(0, 0, 0, 0.45)');
}
})
.state('signup.welcome', {
url: "/welcome",
views: {
modal: {
templateUrl: 'main/signup/welcome.html',
controller: 'signupWelcome'
}
}
}).state('signup.step1', {
url: "/step1",
views: {
modal: {
templateUrl: 'main/signup/step1.html',
controller: 'signupStep1'
}
}
})

上面的代码能够打开模态,它也将状态更改为欢迎,但由于某种原因模板没有加载到模态内。
我已将模板分配给 ui-view=modal它应该打开但不是。

请帮我。提前致谢

最佳答案

我终于能够在状态的基础上查看模态。我同意@Obi Onuorah 的观点,即模态的概念是保持状态并根据要求执行某些操作。但是如果你需要在状态的基础上显示模态,这是可能的。

感谢 angular-ui route 的精彩文档
https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views#view-names---relative-vs-absolute-names
这很好地解释了 ui-view 的绝对命名与相对命名之间的区别。

这是 UI 路由的示例代码

    $stateProvider
.state('list', {
url: '/',
template: '<ul><li ng-repeat="test in testObj"><a ui-sref="view({id: test.id})">{{ test.name }}</a></li></ul>',
controller: function($scope) {
$scope.testObj = testObj;
}
})
.state('modal', {
abstract: true,
parent: 'list',
url: '',
onEnter: ['$modal', '$state', function($modal, $state) {
console.log('Open modal');
$modal.open({
template: '<div ui-view="modal"></div>',
backdrop: false,
windowClass: 'right fade'
}).result.finally(function() {
$state.go('list');
});
}]
})
.state('view', {
url: ':id',
parent: 'modal',
views: {
'modal@': {
template: '<h1>{{ test.name }}</h1><br />\
<a ui-sref="edit({id: test.id})">Edit</a><br />\
<a href="#" ng-click="$close()">Close</a>',
controller: function($scope, test) {
$scope.test = test;
},
resolve: {
test: function($stateParams) {
return testObj[$stateParams.id];
}
}
}
}
})
.state('edit', {
url: ':id/edit',
parent: 'modal',
views: {
'modal@': {
template: '<h1>Edit {{ test.name }}</h1><br /> \
<a ui-sref="view({id: test.id})">View</a> <br />\
<a href="#" ng-click="$close()">Close</a>',
controller: function($scope, test) {
$scope.test = test;
},
resolve: {
test: function($stateParams) {
return testObj[$stateParams.id];
}
}
}
}
});

我做了一个简单的plunker。
http://plnkr.co/edit/hw9x7B?p=preview

关于angularjs - 使用 Angular UI 路由和 UI 模态在 AngularJS 中创建有状态的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com