ui-sref或$ state.go不在第一个指令上工作

时间:2015-05-26 18:35:53

标签: javascript angularjs angularjs-directive angular-ui-router

我在指令上有一个ng-repeat,我传递了3条信息。该指令有一个按钮,我将这些信息传递给另一个使用params的视图:

ui-sref='profiles.show({userId:profile._id, index:index, list:list})

我遇到的问题是所有卡的按钮(卡都是指令)都正常工作,除了索引为0的按钮。该按钮不会执行ui-sref或{{ 1}}。我尝试使用ng-click和控制台记录所有参数,控制台日志显示正确的数据,但带有参数的$state.go没有触发。

$state.go

此外,如果我从params中删除索引和列表,第一个元素的ui-sref或$ state.go再次起作用,但在下一个视图中我不再获得必要的信息。

以下是$state.go('profiles.show', {userId: profile._id, index: index, list: list})

的状态配置
profiles.show

这是ng-repeat(我正在使用Jade):

.state('profiles.show', {url: '/{userId}', params: { index: null, list: null }, templateUrl:'/views/profiles/show/show.html', controller: 'ProfilesShowController'})

这是带有.text-capitalize(al-card, ng-repeat='profile in profiles | filter:searchBar track by $index', profile='profile', index='$index', list='profiles')

的按钮
ng-click

$ scope函数:

button.btn.btn-default(ng-click='goToProfile(profile, index, list)') Learn More

对此有何帮助?

2 个答案:

答案 0 :(得分:1)

问题(正如@KevinF所提到的)是索引0被视为一个假值,要解决它我只是修改了这一行:

- refresh

为:

.text-capitalize(al-card, ng-repeat='profile in profiles | filter:searchBar track by $index', profile='profile', index='$index', list='profiles')

在指令中我只是用.text-capitalize(al-card, ng-repeat='profile in profiles | filter:searchBar track by $index', profile='profile', index='$index.toString()', list='profiles')

将其重新分配给自己

答案 1 :(得分:0)

我在这里做了一个plunkr,它似乎与索引0一起工作,所以我不确定问题是否是索引。除了过滤之外,你有没有看到我的不同之处?我认为过滤可能是问题的一部分

http://plnkr.co/edit/lhU8A3rhUR0JLw5KCNQM

<!DOCTYPE html>
<html ng-app="Test">

  <head>
    <script data-require="angular.js@~1.4.0-rc.2" data-semver="1.4.0-rc.2" src="https://code.angularjs.org/1.4.0-rc.2/angular.js"></script>
    <script data-require="ui-router@*" data-semver="0.2.15" src="//rawgit.com/angular-ui/ui-router/0.2.15/release/angular-ui-router.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ui-view></div>
  </body>

</html>
-------------------------------------------
<div ng-controller="SelectProfile">
  <button ng-repeat="profile in profiles track by $index" 
  ui-sref="profile({userId: profile.userId, index: $index})">
    {{ profile.userId }}
  </button>
</div>
----------------------------------------------
<div ng-controller="Profile">
  {{ profile.userId }}
  <br>
  {{ profile.index }}
</div>

<a ui-sref="home">home</a>
-----------------------------------------------
(function(){
  angular.module('Test', ['ui.router']);

  angular.module('Test')
    .run(function($rootScope, $state, $stateParams){
      $rootScope.$state = $state;
      $rootScope.$stateParams = $stateParams;
    });

  angular.module('Test')
    .config(function($stateProvider, $urlRouterProvider){
      $urlRouterProvider.otherwise('/');

      $stateProvider
            .state('home', {
                url: '/',
                templateUrl: 'home.html'
            })
            .state('profile', {
              url: '/:userId',
              params: {
                index: null,
                list: null
              },
              templateUrl: 'profile.html'
            });
    });

  angular.module('Test')
    .controller('SelectProfile', function($scope){
      $scope.profiles = [
        {userId: 1},
        {userId: 2},
        {userId: 3}
      ];
    });

  angular.module('Test')
    .controller('Profile', function($scope, $rootScope){
      $scope.profile = {
        userId: $rootScope.$stateParams.userId,
        index: $rootScope.$stateParams.index
      };
    });
})();