Angularjs - 在调用子状态时再次阻止非抽象父状态加载

时间:2015-12-10 11:29:44

标签: javascript angularjs angular-ui-router ng-dialog

我试图创建一个维持状态的模态。

每当调用子状态时,都会再次加载父状态(第二次)。是否可以在调用子状态时阻止加载父状态?

这是代码。如果需要更多信息,请发表评论。

.state('parent', {
    url: "/name/:id/:param1",
    templateUrl: "app/partials/parent.html",
    params: {
        param1: {
            value: null,
            squash: true
        }
    }
})
.state('parent.child', {
    url: "/:child",
    template: "<div class='some_name'></div>",
    onEnter: function (ngDialog, $state, $stateParams) {
        ngDialog.open({
            templateUrl: '/app/partials/dialog.html'
        }).closePromise.then(function () {
            $state.go('name', $stateParams);
        });
    }
});

parent.html

<a class="some_class" 
    ui-sref="parent.child({id: id, param1: param1?param1:null,child: 'child'})">
    Open Child
</a>

我试图在子状态下显示模态,当模态关闭时,应用程序返回父状态。

1 个答案:

答案 0 :(得分:1)

您可以将父控制器的逻辑移动到某种activate函数中,如果您还没有,请在致电activate()之前进行检查。

这是一个简化的例子:

.state('parent', {
    url: "/parent",
    template: "<div class='parent'></div>",
    controller: ParentController,
  })
  .state('parent.child', {
    url: "/child",
    template: "<div class='child'></div>",
    controller: ChildController
  });

function ParentController($state) {
  if ($state.is('parent')) {
    activate();
  }
}

ParentController.prototype.activate = function() {
  // load up resources, etc
}

function ChildController() {

}