使用 AngularJS 将一个 md-Select 的 ng-model 中的 JSON Object 复制到另一个 md-Select
Copy JSON Object in ng-model of One md-Select to another md-Select using AngularJS
我有一个 JSON Collection
$scope.person = [
{
"Id": 1
"Name": "John"
},
{
"Id": 2
"Name": "Jack"
},
{
"Id": 3
"Name": "Watson"
},
];
请参考我的post
Here I'm using Angular Material md-select instead of HTML Select
在md-select
中,ng-options不工作。所以,请帮助我如何将第一个 md-select 的 Select 值更新为第二个 md-select
完整的HTML源代码是
<!DOCTYPE html>
<html>
<head>
<title>HTML Select using AngularJS</title>
<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>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="md-block">
<md-input-container>
<label>Person</label>
<md-select ng-model="selected.person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
<hr />
<div class="md-block">
<md-input-container>
<label>Copy Person</label>
<md-select ng-model="selected.copy_person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
</div>
<script>
var app = angular.module('myApp', ['ngMaterial'])
app.controller('myCtrl', function ($scope) {
$scope.person = [
{
"Id": 1,
"Name": "John"
},
{
"Id": 2,
"Name": "Jack"
},
{
"Id": 3,
"Name": "Watson"
}
];
$scope.selected = {
person: null,
copy_person:null
};
$scope.$watchCollection('selected.person', function (newData, oldDaata) {
var obj = JSON.parse(newData);
if ((obj != undefined) && (obj != null) && (obj.Id != undefined) && (obj.Id != null) && (obj.Id != "0")) {
var name = obj.Name;
alert(name);
$scope.selected.copy_person = obj;
}
});
});
</script>
</body>
</html>
我有两个 Angular 材料 md-Select 具有相同的 JSON Collection。我 Select 在第一个 md-select "Person" 中编辑了一个 Person Watson,然后我需要在第二个 md-select "Copy Person" 中更新 Same。但是更新不了
我将 JSON Object 绑定为 md-select 选项中的值,而不是 Id 或 Name
请帮助我如何在 md-select 中更新?
很遗憾 md-select 不支持 ng-options...
并且 ng-repeat 无法与 select 组件的 ng-model 绑定。
不是分配对象,而是分配原始值。
您目前正在使用这种方法:
$scope.selected.copy_person = JSON.Parse(newData);
只需将逻辑更改为
$scope.selected.copy_person = newData;
完整版本是
<!DOCTYPE html>
<html>
<head>
<title>HTML Select using AngularJS</title>
<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>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="md-block">
<md-input-container>
<label>Person</label>
<md-select ng-model="selected.person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
<hr />
<div class="md-block">
<md-input-container>
<label>Copy Person</label>
<md-select ng-model="selected.copy_person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
</div>
<script>
var app = angular.module('myApp', ['ngMaterial'])
app.controller('myCtrl', function ($scope) {
$scope.person = [
{
"Id": 1,
"Name": "John"
},
{
"Id": 2,
"Name": "Jack"
},
{
"Id": 3,
"Name": "Watson"
}
];
$scope.selected = {
person: null,
copy_person:null
};
$scope.$watchCollection('selected.person', function (newData, oldDaata) {
var obj = JSON.parse(newData);
if ((obj != undefined) && (obj != null) && (obj.Id != undefined) && (obj.Id != null) && (obj.Id != "0")) {
var name = obj.Name;
alert(name);
$scope.selected.copy_person = newData;
}
});
});
</script>
</body>
</html>
默认情况下,值部分将 JSON 对象分配为字符串,根据您的 post 。因此,只需将值作为字符串而不是 JSON 对象进行比较。
我有一个 JSON Collection
$scope.person = [
{
"Id": 1
"Name": "John"
},
{
"Id": 2
"Name": "Jack"
},
{
"Id": 3
"Name": "Watson"
},
];
请参考我的post
Here I'm using Angular Material md-select instead of HTML Select
在md-select
中,ng-options不工作。所以,请帮助我如何将第一个 md-select 的 Select 值更新为第二个 md-select
完整的HTML源代码是
<!DOCTYPE html>
<html>
<head>
<title>HTML Select using AngularJS</title>
<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>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="md-block">
<md-input-container>
<label>Person</label>
<md-select ng-model="selected.person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
<hr />
<div class="md-block">
<md-input-container>
<label>Copy Person</label>
<md-select ng-model="selected.copy_person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
</div>
<script>
var app = angular.module('myApp', ['ngMaterial'])
app.controller('myCtrl', function ($scope) {
$scope.person = [
{
"Id": 1,
"Name": "John"
},
{
"Id": 2,
"Name": "Jack"
},
{
"Id": 3,
"Name": "Watson"
}
];
$scope.selected = {
person: null,
copy_person:null
};
$scope.$watchCollection('selected.person', function (newData, oldDaata) {
var obj = JSON.parse(newData);
if ((obj != undefined) && (obj != null) && (obj.Id != undefined) && (obj.Id != null) && (obj.Id != "0")) {
var name = obj.Name;
alert(name);
$scope.selected.copy_person = obj;
}
});
});
</script>
</body>
</html>
我有两个 Angular 材料 md-Select 具有相同的 JSON Collection。我 Select 在第一个 md-select "Person" 中编辑了一个 Person Watson,然后我需要在第二个 md-select "Copy Person" 中更新 Same。但是更新不了
我将 JSON Object 绑定为 md-select 选项中的值,而不是 Id 或 Name
请帮助我如何在 md-select 中更新?
很遗憾 md-select 不支持 ng-options...
并且 ng-repeat 无法与 select 组件的 ng-model 绑定。
不是分配对象,而是分配原始值。
您目前正在使用这种方法:
$scope.selected.copy_person = JSON.Parse(newData);
只需将逻辑更改为
$scope.selected.copy_person = newData;
完整版本是
<!DOCTYPE html>
<html>
<head>
<title>HTML Select using AngularJS</title>
<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>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="md-block">
<md-input-container>
<label>Person</label>
<md-select ng-model="selected.person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
<hr />
<div class="md-block">
<md-input-container>
<label>Copy Person</label>
<md-select ng-model="selected.copy_person">
<md-option ng-repeat="key in person | orderBy:Id" value="{{key}}">({{key.Name}})</md-option>
</md-select>
</md-input-container>
</div>
</div>
<script>
var app = angular.module('myApp', ['ngMaterial'])
app.controller('myCtrl', function ($scope) {
$scope.person = [
{
"Id": 1,
"Name": "John"
},
{
"Id": 2,
"Name": "Jack"
},
{
"Id": 3,
"Name": "Watson"
}
];
$scope.selected = {
person: null,
copy_person:null
};
$scope.$watchCollection('selected.person', function (newData, oldDaata) {
var obj = JSON.parse(newData);
if ((obj != undefined) && (obj != null) && (obj.Id != undefined) && (obj.Id != null) && (obj.Id != "0")) {
var name = obj.Name;
alert(name);
$scope.selected.copy_person = newData;
}
});
});
</script>
</body>
</html>
默认情况下,值部分将 JSON 对象分配为字符串,根据您的 post