简体   繁体   English

从子级返回时,AngularJS父控制器未调用

[英]AngularJS parent controller is not invoking while going back from child

I have AngluarJS view with listing of some items, user can delete/update each item by clicking on it. 我有一些项目列表的AngluarJS视图,用户可以通过单击delete/update每个项目。 When user click one line item we are showing a child view . 当用户单击一个订单项时,我们将显示一个child view When user do some delete/update action on the line item I am re-loading the parent view using state.go function. 当用户对订单项执行某些删除/更新操作时,我正在使用state.go函数重新加载父视图。

But my parent view controller is not getting called( statements in parent controller is not getting invoked). 但是我的父视图控制器没有被调用(父控制器中的语句没有被调用)。

I have used $urlRouterProvider to manage each state. 我已经使用$urlRouterProvider来管理每个状态。 I am going back to parent state by using following statement: 我通过使用以下语句返回到父状态:

$state.go('parent',{}, {reload: true});

Above action is triggering my configuration snippet, and loading new data from server, following is the code snippet in configuration to load parent state: 上面的操作触发了我的配置代码段,并从服务器加载新数据,以下是配置中的代码段以加载父状态:

$stateProvider.state('parent', {
            url                 : '/parent',
            templateUrl         :  'app/parent.html',
            controller          :  'ParentController',
            controllerAs        :  'parentController',
            resolve         : {
                list : function($stateParams, parentServices){
                    return parentServices.getList();
                }

            }
        });

parentServices.getList() this function is getting invoked. parentServices.getList()正在调用此函数。 I have put break point for the first statement in the parent controller, but its not getting invoked. 我已经在父控制器中为第一条语句设置了断点,但是没有被调用。

While reloading the page its loading fine. 重新加载页面时,其加载正常。 Please let me know what's wrong with the current flow, which is blocking from calling controller. 请让我知道当前流出了什么问题,这阻止了调用控制器。

Update : State configruations 更新:状态配置

$stateProvider.state('parent', {
            url                 : '/parent',
            templateUrl         :  'app/parent-management/parent.html',
            controller          :  'ParentController',
            controllerAs        :  'parentController',
            resolve         : {
                parentList : function($stateParams, keyServices){
                    return parentServices.getList();
                }

            }
        });
        $stateProvider.state('parent.child', {
            url             : '/:action/:id',
            templateUrl     : 'app/parent-management/parent-child.html',
            controller      : 'ParentChildController',
            controllerAs    : 'parentChildController',
            resolve         : {
                currentModelID : function($stateParams){
                    return {
                        id      : $stateParams.id,
                        action  : $stateParams.action
                    }
                }
            }
        });

If I navigate to a child state the child controller will be initialised, if I then navigate back to the parent state, the parent controller will not be re-created. 如果导航到子状态,则将初始化子控制器,如果再导航回父状态,则不会重新创建父控制器。

Thanks. 谢谢。

Place your list of items inside an Angular service you can then use this service to access the data in both your parent and child controllers. 将项目列表放在Angular服务中,然后可以使用此服务访问父控制器和子控制器中的数据。 Make the service responsible for updating (make sure you update and don't replace) the list with information from the server. 使服务负责使用来自服务器的信息来更新(确保您更新并且不替换)列表。

You can then use this information in both your controllers and any manipulation each controller makes on the data will be available to both controllers without the need to re-initialise the controller (which won't happen for the parent controller when using nested states). 然后,您可以在两个控制器中使用此信息,并且每个控制器对数据进行的任何操作都可用于两个控制器,而无需重新初始化控制器(使用嵌套状态时,父控制器不会发生这种情况)。

Well, the structure is the most important part in Angular apps and router play a vital role in this. 好吧,结构是Angular应用程序中最重要的部分,而路由器在其中起着至关重要的作用。

        $stateProvider
            .state("versions-layout", {
                abstract: true,
                url: "/versions/:id",
                templateUrl: "js/versions/versions-layout.html",
                controller: "VersionsController",
                controllerAs: "versionsVm",
                resolve: {
                    version: ["$stateParams", "VersionsService", function ($stateParams, VersionsService) {
                        return VersionsService.getVersionById($stateParams.id);
                    }]
                },
            })
            .state('versions', {
                parent: "versions-layout",
                url: "",
                views: {
                    "version-characteristics": {
                        templateUrl: 'js/versions/version-characteristics/version-characteristics.html',
                        controller: 'VersionCharacteristicsController',
                        controllerAs: 'vm'
                    },
                    "programs": {
                        templateUrl: 'js/versions/programs/programs.html',
                        controller: 'ProgramsController',
                        controllerAs: 'vm',
                        resolve: {
                            programsData: ["version", function (version) {
                                return version.data.programs;
                            }]
                        }
                    }
})

I was able to resolve my problem by doing the following changes in my code: 通过在代码中进行以下更改,我能够解决问题:

My child view was inside a modal popup , so to go back to parent view from child view (with reloading of data), the code snippet was written as follows: 我的child view位于modal popup ,因此要从child view返回parent view child view (并重新加载数据),代码片段的编写方式如下:

$state.go(states.parent,{}, {reload: true});
closeChilUIPopover();// functionality to close the modal popup of child

When I commented out closeChilUIPopover() function, parent view was rendering fine with newly loaded data. 当我注释掉closeChilUIPopover()函数时,父视图使用新加载的数据可以很好地呈现。

So then I changed order of call as follows: 因此,我更改了通话顺序,如下所示:

closeChilUIPopover();
$state.go(states.parent,{}, {reload: true});

But after changing the order of call also had same issue. 但是更改通话顺序后也有同样的问题。 So I have added the closeChildUIPopover in callback function of $state.go as follows: 所以我加入了closeChildUIPopovercallback函数$state.go如下:

$state.go(states.parent,{}, {reload: true}).then(function(){
    closeChilUIPopover();
});

Now everything is working fine. 现在一切正常。 Thanks for all help. 感谢所有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM