angularjs - 下拉菜单中的 Angular ui-sref 不起作用

标签 angularjs

我有一个带路由的 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>

当我选择其中一个下拉菜单时,不知何故它没有加载我正在引用的 View 。我哪里出错了?我也试过这个:
<option ui-sref="creditcard">Credit</option>

最佳答案

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

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

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

就像是:

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

您的 Controller 可能如下所示:
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 函数

关于angularjs - 下拉菜单中的 Angular ui-sref 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212244/

相关文章:

javascript - 如何使用正则表达式在 angularjs 上打印值?

javascript - 必需的字符串参数 'text' 不存在

javascript - Node & Express 中的 Js 文件显示 HTML 文件

javascript - 如何使用 Ranorex 以编程方式检查 ng-model 绑定(bind)输入字段的值?

javascript - ng-repeat 执行多次

javascript - 在 Angular js中将值从一个控件传递到另一个控件

javascript - 如何将服务注入(inject) AngularJS 的 UI-Router templateUrl 函数?

javascript - 无法读取未定义的属性 'ClearCredentials'

javascript - 在我的例子中,AngularJS undefined 不是一个函数错误

javascript - 在 AngularJS 中从另一个服务调用一个服务