微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过ng-click选择选项在使用AngularJS的chrome浏览器中不起作用

我需要使用ng-click以便我可以传递三个参数并根据选择在我的本地json中设置.

<select>                                                             
<option id="" value="">--Select--</option> 
<option ng-repeat="comparison in comparisons" ng-click="setSkillComparisonoperator(index,comparison.sid,comparison.name)"
 ng-selected="comparison.sid == data.value.comparisonoperator.sid">{{comparison.name}}</option>
</select>

我可以使用ng-model和ng-change如下.

<select ng-model="item" ng-options="o.id as o.id for o in list" ng-change="onFunction(item)">{{item}}</select>

在这种情况下,我不能传递如下三个参数.

<select ng-model="item" ng-options="o.id as o.id for o in list" ng-change="onFunction(o.id,o.name)">{{item}}</select>

解决方法

最后,我找到了解决方案.

JS fiddle link

HTML页面

<div ng-app="miniapp">
    <div ng-controller="Ctrl">
        <select ng-model="selectedColor" ng-options="color as color.name for color in colors" ng-init="selectedColor = check(color1,colors)" ng-change="setColor(selectedColor)">
            <option value="">Select A Color</option>
        </select>
        <br/>
        <br/>
        Color Id:    <span ng-bind="colorId"></span>
        <br/>
        Color name:    <span ng-bind="color"></span>
        <br/>
        Color shade:    <span ng-bind="shade"></span>
        <br/>
    </div>
</div>

Javascript是

var $scope;
var app = angular.module('miniapp',[]);

function Ctrl($scope) {
    $scope.colorId = 4;
    $scope.shade = null;
    $scope.color = null;
    $scope.color1 = {name:'Red',value: 'red',id:2};
    $scope.colors = [
        {name:'Red',shade: 'white',id:1},{name:'Orange',shade: 'red',id:2},{name:'Yellow',shade: 'blue',id:3},{name:'Green',shade: 'yellow',id:4},{name:'Blue',shade: 'indigo',id:5},{name:'Indigo',shade: 'violet',id:6},{name:'Violet',shade: 'orange',id:7}
     ];
    $scope.check =function(selectedColor,colors){
        var i = null;
        for(i in colors){
            if(colors[i].id == selectedColor.id){
                return colors[i];
            }
        }
    };
    $scope.setColor= function(color){
        $scope.colorId = color.id;
        $scope.shade = color.shade;
        $scope.color = color.name;
    };
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐