Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select options through ng-click is not working in chrome browser using AngularJS

Tags:

angularjs

I need to use ng-click so that I can pass three parameters and set in my local json based on the selection.

<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>

I can use it ng-model and ng-change as follows.

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

In that case , I can't pass three parameters as follows.

<select ng-model="item" ng-options="o.id as o.id for o in list" ng-change="onFunction(o.id,o.name)">{{item}}</select>
like image 630
Krish Lakshmanan Avatar asked Jan 10 '14 18:01

Krish Lakshmanan


1 Answers

At last , I found solution.

JS fiddle link

HTML page is

<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 is

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;
    };
};
like image 104
Krish Lakshmanan Avatar answered Nov 05 '22 06:11

Krish Lakshmanan