Ich muss eine Eigenschaft in einer Auflistung aktualisieren, wenn eine andere Eigenschaft geändert wird.OnSelect-Ereignis in md-select, wenn ng-model in AngularJS aktualisiert wird
Meine Sammlung ist
$scope.Persons = [
{
Name: "Emma",
Mode:0,
HasModified:false
},
{
Name: "Watson",
Mode:0,
HasModified:false
},
{
Name: "Harry",
Mode:0,
HasModified:false
}
];
Die Liegenschaft Mode
in einem md-select
binded, wenn der Benutzer eine Option wählt, dann muss ich true
HasModified
aktualisieren
HTML-Abschnitt:
<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>
Die onselect
Ereignis nicht die Änderungen
$scope.UpdateMode = function(collection) {
if((collection != null) && (collection != undefined) && (collection.Mode >0)) {
collection.HasModified = true;
}
}
Die komplette HTML Angular Source Code ist
<!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;
}
}
});
\t
</script>
</body>
</html>
Bitte helfen Sie mir zu aktualisieren, wiezu aktualisierenHasModified
ng-Wiederholung erstellt standardmäßig einen neuen Bereich. In diesem Fall erhält die UpdateMode-Funktion ein nicht identifiziertes Argument. Sie müssen Ihre Architektur in diesem Fall erneut besuchen. –