2017-05-06 43 views
0

我有一個對象的列表,我將參數傳遞給一個狀態。鏈接(href)正確創建,但是當我更改列表時,舊的hrefs保留而不是生成新的值。如何從ui路由器更新href與data-ui-state/data-ui-state-params

<ul> 
    <li data-ui-sref-active="active" data-ng-repeat="location in locations track by $index"> 

    <a data-ui-state="'.map.location'" data-ui-state-params="{ 'descriptor': '{{location.descriptor}}'}"> 

     {{location.descriptor}} 
    </a> 

</ul> 
<button ng-click="changeList()">change list</button> 

和控制器:

var app = angular.module('plunker', ['ui.router']) 
    .config(function($stateProvider, $urlRouterProvider) { 
    $stateProvider 

     // route for the home page 
     .state('map', { 
      url:'/map', 
      templateUrl:'map.html' 
     }) 

     .state('map.location', { 
      url:'/location/{descriptor}', 
      templateUrl:'map.html' 
     }) 

    $urlRouterProvider.otherwise('/map'); 
}); 


app.controller('MainCtrl', function($scope) { 

    $scope.locations = [{ 
    descriptor: "1" 
    },{ 
    descriptor: "2" 
    },{ 
    descriptor: "3" 
    },{ 
    descriptor: "4" 
    },{ 
    descriptor: "5" 
    }]; 

    $scope.changeList = function(){ 

    $scope.locations = [{ 
     descriptor: "A" 
    },{ 
     descriptor: "B" 
    },{ 
     descriptor: "C" 
    }]; 
    } 

}); 

全部代碼在這裏:http://plnkr.co/edit/s4W3Qa0w37LO7HnwAZbH

+0

請參閱:http://stackoverflow.com/questions/29849293/ng-repeat-not-updating-on-update-of-array –

+0

正確更新視圖上的數據,包括data-ui-state和data-ui-state-params,只有href不會改變。 –

回答

0

我做了這個線程激發了黑客:https://www.reddit.com/r/angularjs/comments/351mg3/uisref_issue_href_wont_update_uisref_does/

app.directive('paramSref', function($compile, $state) { 
    return { 
    restrict: 'A', 
    link: function(scope, element, attrs) { 
     scope.$watch(function(){return element.attr('param-sref');},function(newV, oldV) { 
     if (newV !== oldV) { 
      attrs.$set('href', $state.href('map.location', eval('(' + newV + ')'))); 
     } 
     }); 
    } 
    }; 
}); 
在指數

<a ui-sref=".map.location({ 'descriptor': '{{location.descriptor}}'})" 
    param-sref="{ 'descriptor': '{{location.descriptor}}'}">