【问题标题】:Angular ui-sref on dropdown not working下拉菜单上的 Angular ui-sref 不起作用
【发布时间】:2015-05-26 13:27:32
【问题描述】:

我有一个带路由的 Angular 应用程序。我有一个下拉选择定义如下:

<select class="form-control" id="transactiontype" ng-model="tt">
    <option><a ui-sref="cash">Cash</a></option>
    <option><a ui-sref="cheque">Cheque</a></option>
    <option><a ui-sref="debitcard">Debit</a></option>
    <option><a ui-sref="creditcard">Credit</a></option>
</select>

当我选择其中一个下拉菜单时,不知何故它没有加载我正在引用的视图。我哪里错了?我也试过这个:

<option ui-sref="creditcard">Credit</option>

【问题讨论】:

  • 我们需要查看路由器配置来回答您的问题

标签: angularjs


【解决方案1】:

考虑改用 ng-options:

myApp.controller('MainController', ['$scope', '$state', function($scope, $state) {    
$scope.options = [
  { label: 'Cash', value: 'cash' },
  { label: 'Cheque', value: 'cheque' },
  { label: 'Debit', value: 'debit' },
  { label: 'Credit', value: 'credit' }
];
$scope.$watch('tt', function(value) {
  if(value) {
    $state.go(value);
  }
});
}]);

HTML:

<div ng-controller="MainController">
  <select class="form-control" id="transactiontype" ng-model="tt" ng-options="opt.value as opt.label for opt in options">
  </select>
</div>

编辑:同意@Likwid_T 所说的,我认为 ng-change 是检测选择标签变化的最佳选择。我只是在那一刻忘记了它。所以也请参考他的回答。美好的一天。

【讨论】:

    【解决方案2】:

    我的解决方案最终只使用了通用 href,但使用了带有 '#/' 前缀的旧 ng-router 声明。例如:而不是使用...

    <select class="form-control" id="transactiontype" ng-model="tt">
        <option><a ui-sref="cash">Cash</a></option>
        <option><a ui-sref="cheque">Cheque</a></option>
        <option><a ui-sref="debitcard">Debit</a></option>
        <option><a ui-sref="creditcard">Credit</a></option>
    </select>
    

    尝试使用带有'#/'前缀的href...

    <select class="form-control" id="transactiontype" ng-model="tt">
        <option><a href="#/cash">Cash</a></option>
        <option><a href="#/cheque">Cheque</a></option>
        <option><a href="#/debitcard">Debit</a></option>
        <option><a href="#/creditcard">Credit</a></option>
    </select>
    

    【讨论】:

      【解决方案3】:

      我很确定这是因为 a 标签在 select - option 标签内不起作用。

      根据这个 SO 问题:using href link inside option tag

      尝试编写一个函数来重定向页面并根据选择更改或模型更改触发它,无论它最适合您的程序。

      类似:

      <select class="form-control" id="transactiontype" ng-model="tt" ng-change="locationChangeFunction()">
      ...
      </select>
      

      您的控制器可能如下所示:

      angular.module('myApp')
      .controller('myController', ['$scope', '$state', function ($scope, $state){
      
          $scope.locationChangeFunction = function(){
               $state.go($scope.tt);
          }
      }]);
      

      编辑

      结合使用这两种解决方案(归功于 Hieu TB):

      <select class="form-control" id="transactiontype" ng-model="tt" ng-options="opt.value as opt.label for opt in options" ng-change="locationChangeFunction()"></select>
      

      ng-change 将等待模型更改,这将更改 DOM,这将触发更改,现在您没有运行消耗资源的 $watch 函数

      【讨论】:

      • @Hieu TB 的方法可能更可靠,你必须尝试一下,我宁愿对事件进行更改,因为 .$watch 本质上是一个运行所有时间,而在事件(如 ng-change)中,您只在需要时运行该函数。但是我不知道 ng-change 是否会在模型更改之前或之后触发,即使是这样,您可能只是有竞争条件,但是使用 $watch 您知道模型已更改。我仍然会尝试找到一种方法来通过事件而不是观看来做到这一点,因为如果您出于任何其他原因更改模型,那么您将再次更改页面。
      • 感谢@Likwid_T 指出这一点。我认为“ng-change”也是最好的,只是因为我太老鹰回答问题而忘记了它。我已经对你的回答投了赞成票,:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多