从子对象填充表单
Populate form from child object
我尝试寻找解决方案,但找不到确切的解决方案。
这是场景。
我有一个对象列表ctrl.Parents
父对象包含一个子对象列表,就像我们有一个 poco 实体一样。
我想要的是,从子对象填充一个选择下拉列表。这是一个不完整的 plunker,以下是可以给你一个想法的代码。
// Code goes here
var app = angular.module("testApp", []);
app.controller("testCtrl", function($scope) {
$scope.Parents = [{
"Id": 1,
"Name": "First Note",
"TypeName": "First Type",
"Notes": [{
"Id": 1,
"ParentID": 1,
"Draft": "Draft 1",
"Note": "First Draft"
}, {
"Id": 2,
"ParentID": 1,
"Draft": "Draft 2",
"Note": "Second Draft"
}]
}, {
"Id": 2,
"Name": "Second Note",
"TypeName": "Second Type",
"Notes": [{
"Id": 3,
"ParentID": 2,
"Draft": "Draft 1",
"Note": "First Draft"
}, {
"Id": 4,
"ParentID": 2,
"Draft": "Draft 4",
"Note": "Second Draft"
}]
}]
});
<!DOCTYPE html>
<html ng-app="testApp">
<head>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="testCtrl">
<select ng-model="parent.name" ng-options="p.Name for p in Parents">
</select>
<br/>
<b><span>Following list should be populated with Parent.Notes.Name
as show in the <br/>
EX: If Parent drop down has value First Note then following list will show</span></b>
<br/>
<select>
<option selected="selected" value="Draft 1">Draft 1</option>
<option value="Draft 2">Draft 2</option>
</select>
<br/>
<span>And text will be as follows</span>
<br/>
<textarea>First Draft</textarea>
</body>
</html>
我看了看你的 plunker,这几乎是对的。您正在尝试绑定到 Notes 模型上不存在的属性。试试这个:
<!DOCTYPE html>
<html ng-app="testApp">
<head>
<link rel="stylesheet" href="style.css">
<script data-require="angular.js@*" data-semver="1.3.1" src="//code.angularjs.org/1.3.1/angular.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="testCtrl">
<select ng-model="parent" ng-options="p.Name for p in Parents">
</select>
<select ng-options="n.Note for n in parent.Notes" ng-model="Notes">
</select>
</body>
</html>
如果要选择默认值,请将其添加到模型中:
...//the end of your model
}, {
"Id": 4,
"ParentID": 2,
"Note": "Second Draft"
}]
}]
$scope.parent = $scope.Parents[0];
});
为了根据属性的值选择默认子项,您必须相应地更新代码:
将脚本.js更改为:
// Code goes here
var app = angular.module("testApp", []);
app.controller("testCtrl", function($scope) {
$scope.Parents = [{
"Id": 1,
"Name": "First Note",
"TypeName": "First Type",
"Notes": [{
"Id": 1,
"ParentID": 1,
"Note": "First Draft",
"Def" : true
}, {
"Id": 2,
"ParentID": 1,
"Note": "Second Draft",
"Def" : false
}]
}, {
"Id": 2,
"Name": "Second Note",
"TypeName": "Second Type",
"Notes" : [{
"Id": 3,
"ParentID": 2,
"Note": "First Draft",
"Def" : false
}, {
"Id": 4,
"ParentID": 2,
"Note": "Second Draft",
"Def" : true
}]
}];
$scope.update = function() {
$scope.Notes = $scope.findNote($scope.parent.Notes);
}
$scope.findNote = function (notes) {
for (var i = 0; i < notes.length; i++) {
if (notes[i].Def == true) {
return notes[i];
}
}
}
});
并更新您的 HTML:
<body ng-controller="testCtrl">
<select ng-model="parent" ng-options="p.Name for p in Parents" ng-change="update()">
</select>
<select ng-options="n.Note for n in parent.Notes" ng-model="Notes" >
</select>
</body>
这是有帮助吗?
普罗提
<body ng-controller="testCtrl">
<select ng-model="p" ng-options="p.Name for p in Parents">
</select>
<select ng-model="note.SelectedID" ng-options="note.Id for note in p.Notes">
</select>
</body>
相关文章:
- 单击鼠标,用MySQL数据填充html表单输入字段
- 通过浏览器对表单自动填充做出反应
- 谷歌表单-根据分配给谷歌应用程序的工作为商业用户预先填充多项选择列表
- 提交表单时,Javascript(JQuery)不会在文本区域中填充内容
- Javascript未在开发服务器上填充Perl表单
- 如何在js中实现树结构类型的表单动态填充
- CasperJS填充表单-输入名称有方括号
- 如何使用多维JSON数据重新填充表单
- 试图用cookie中的信息填充表单
- 如何在刷新时重新填充表单中的复选框并将表单值发布到页面
- 单击一个数据表,填充第二个数据表
- 将表单填充到要与ngMessages一起使用的templateURL中
- jQuery表单填充器不工作
- Rails 输入表单填充了 jQuery .val(),但参数中不存在数据
- 使用自动填充表单控件(自动完成属性),加快表单填充速度
- HTML5地理位置地址表单填充器未捕获语法错误:意外的标记<
- 使用AJAX调用进行表单填充和验证
- 在检索数据时,表单填充然后清除
- 基于浏览器的客户端表单填充
- 从Google表单填充HTML表单