AngularJS: Multiple views with routing without losing scope

前端 未结 4 943
陌清茗
陌清茗 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:21

    I've found Angular Multi View to be a godsend for this scenario. It lets you preserve scope as the route changes and lets multiple controllers share the same route without nesting your views.

    I recommend Angular Multi View if you have more than 2 views on your page. Otherwise, when using ui-router, nesting multiple views gets messy really fast.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 11:29

    Try using ui-router: http://github.com/angular-ui/ui-router.

    They have nested views and easier state management than angular default routing :-)

    0 讨论(0)
  • 2021-02-04 11:38

    Multiple views are not supported in core AngularJS. You can use this library for this purpose which supports any amount of nested views on the page, where each level is configured independently with its own controller and template:

    http://angular-route-segment.com

    It is much simpler to use than ui-router. Sample config may look like this:

    $routeSegmentProvider.
    
    when('/section1',          's1.home').
    when('/section1/prefs',    's1.prefs').
    when('/section1/:id',      's1.itemInfo.overview').
    when('/section1/:id/edit', 's1.itemInfo.edit').
    when('/section2',          's2').
    
    segment('s1', {
        templateUrl: 'templates/section1.html',
        controller: MainCtrl}).
    
    within().
    
        segment('home', {
            templateUrl: 'templates/section1/home.html'}).
    
        segment('itemInfo', {
            templateUrl: 'templates/section1/item.html',
            controller: Section1ItemCtrl,
            dependencies: ['id']}).
    
        within().
    
            segment('overview', {
                templateUrl: 'templates/section1/item/overview.html'}).
    
            segment('edit', {
                 templateUrl: 'templates/section1/item/edit.html'}).
    
            up().
    
        segment('prefs', {
            templateUrl: 'templates/section1/prefs.html'}).
    
        up().
    
    segment('s2', {
        templateUrl: 'templates/section2.html',
        controller: MainCtrl});
    
    0 讨论(0)
提交回复
热议问题