AngularJS: Multiple views with routing without losing scope

前端 未结 4 947
陌清茗
陌清茗 2021-02-04 11:07

I\'m trying to implement a classic list/details UI. When clicking an item in the list, I want to display an edit form for that item while still displaying the list. I\'

4条回答
  •  粉色の甜心
    2021-02-04 11:27

    I came up with the same problem and I personnaly don't like plugins when they aren't absolutely unavoidable. I just moved singleton part to a service.

    In my case there are :id[/:mode] routes and I want to react different way if user changes just mode or id too. Thus, I have to know previous id.

    So, there is a service with activate method which updates its state. And the scope is reinitialized every time with the following code.

    module.controller('MyController', ['$scope', '$routeParams', 'navigator', function($scope, $routeParams, navigator) {
        var id = null;
        var mode = null;
    
        if (typeof($routeParams.id)!='undefined')
        {
            id = $routeParams.id;
        }
    
        if (typeof($routeParams.mode)!='undefined')
        {
            mode = $routeParams.mode;
        }
    
        navigator.activate(id, mode);
    
        $scope.items = navigator.items;
        $scope.activeItem = navigator.activeItem;
        $scope.modes = navigator.modes;
        $scope.activeMode = navigator.activeMode;
    }]);
    

    In activate method I can compare id to the singleton's activeItem.id and react differently.

提交回复
热议问题