JavaScript 和 ES6
在这个过程你会发现,有很多 JS 知识点你并不能更好的理解为什么这么设计,以及这样设计的好处是什么,这就逼着让你去学习这单个知识点的来龙去脉,去哪学?第一,书籍,我知道你不喜欢看,我最近通过刷大厂面试题整理了一份前端核心知识笔记,比较书籍更精简,一句废话都没有,这份笔记也让我通过跳槽从8k涨成20k。
如果你觉得对你有帮助,可以戳这里获取:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
“myOptions”: [
{
“id”: 106,
“group”: “Group 1”,
“label”: “Item 1”
},
{
“id”: 107,
“group”: “Group 1”,
“label”: “Item 2”
},
…
}
// Get stream in the object called data
$scope.myOptions = data.myOptions;
The good thing is, you have a pretty flat data stream. When I started with this feature, I had an array of groups each containing the specific items.
It was not possible for me to fill a select box that way. Instead, I refactored some of my code to what you can see above.
Angular.js would take care of the grouping on it’s own.
Here is my select definition:
<select
ng-model=“myOption”
ng-options=“value.id as value.label group by value.group for value in myOptions”>
–
ng-model is the name of the property which will reference the selected option. ng-options is the directive which fills the dropdown. It deserves a bit more attention.
You will understand it more easily if you read it from right to left. First there is:
for value in myOptions
It means you’ll iterate through elements which are stored in myOptions. Every element will become available in this expression with the name “value”.
The next part is:
group by value.group
This will tell Angular.js to make up
tags and the label attribute will be filled with the content of the group field.
The last one is:
value.id as value.label
In this case, value.id will become the model (stored in ng-model), if your users have chosen an option. If you would delete “value.id as”, simply the whole value object would become the model.
value.label
does exactly what it looks like: it’s the label of the select box.
If you run your code, you’ll see something like that:
Item 1
JavaScript 和 ES6
在这个过程你会发现,有很多 JS 知识点你并不能更好的理解为什么这么设计,以及这样设计的好处是什么,这就逼着让你去学习这单个知识点的来龙去脉,去哪学?第一,书籍,我知道你不喜欢看,我最近通过刷大厂面试题整理了一份前端核心知识笔记,比较书籍更精简,一句废话都没有,这份笔记也让我通过跳槽从8k涨成20k。