B.Balamanigandan
B.Balamanigandan

Reputation: 4875

OnSelect event in md-select when ng-model gets update in AngularJS

I need to update a property in a collection when another property gets changed.

My Collection is

    $scope.Persons = [
        {
            Name: "Emma",
            Mode:0,
            HasModified:false
        },
        {
            Name: "Watson",
            Mode:0,
            HasModified:false
        },
        {
            Name: "Harry",
            Mode:0,
            HasModified:false
        }
    ];

The Property Mode is binded in a md-select, when the user selects any option, then I need to update the property HasModified to true

HTML Section:

<tbody>
    <tr ng-repeat="main in Persons">
        <td>
            <p>{{main.Name}}</p>
        </td>
        <td>
            <md-select ng-model="main.Mode" onselect="UpdateMode(main)">
                <md-option ng-value="1">Good</md-option>
                <md-option ng-value="2">Bad</md-option>
            </md-select>
        </td>
        <td>
            <p>{{main.HasModified}}</p>
        </td>
    </tr>
</tbody>

The onselect event fails to update the changes

    $scope.UpdateMode = function(collection) {
        if((collection != null) && (collection != undefined) && (collection.Mode >0)) {
            collection.HasModified = true;
        }
    }

The Complete HTML Angular Source Code is

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>

<!-- Angular Material Library -->
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.4/angular-material.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl"> 

<table border="1" cellpadding="2" cellspacing="0">
    <thead>
        <tr>
            <th>Name</th>
            <th>Mode</th>
            <th>Has Modified</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="main in Persons">
            <td>
                <p>{{main.Name}}</p>
            </td>
            <td>
                <md-select ng-model="main.Mode" onselect="UpdateMode(main)" aria-label="{{main.Name}}">
                    <md-option ng-value="1">Good</md-option>
                    <md-option ng-value="2">Bad</md-option>
                </md-select>
            </td>
            <td>
                <p>{{main.HasModified}}</p>
            </td>
        </tr>
    </tbody>
</table>
</div>

<script>
    var app = angular.module('myApp', ['ngMaterial']);

    app.controller('myCtrl', function ($scope, $http, $q) {

        $scope.Persons = [
            {
                Name: "Emma",
                Mode:0,
                HasModified:false
            },
            {
                Name: "Watson",
                Mode:0,
                HasModified:false
            },
            {
                Name: "Harry",
                Mode:0,
                HasModified:false
            }
        ];

        $scope.UpdateMode = function(collection) {
            if((collection != null) && (collection != undefined) && (collection.Mode >0)) {
                collection.HasModified = true;
            }
        }

    });
	
</script>
</body>
</html>

Kindly assist me how to update HasModified Property...

Upvotes: 2

Views: 10045

Answers (1)

dfsq
dfsq

Reputation: 193301

There is no such thing as onselect event supported by mdSelect directive. Use ngChange:

<md-select ng-model="main.Mode" ng-change="UpdateMode(main)" aria-label="{{main.Name}}">
    <md-option ng-value="1">Good</md-option>
    <md-option ng-value="2">Bad</md-option>
</md-select>

Upvotes: 6

Related Questions