javascript - angular2如何雙向綁定多個(gè)checkbox?
問(wèn)題描述
比如我又一個(gè)數(shù)組如下:
var array = [’喜歡’,’不喜歡’,’非常喜歡’,’超級(jí)喜歡’,’喜歡得不得了’];
html模板中
<p *ngFor='let e of array'> <input type='checkbox' name='like' value='{{e}}'></p><p class='youselect'></p>
我蓋如何實(shí)現(xiàn),選中其中一個(gè)checkbox后,能在p.youselect中顯示出我已經(jīng)選中的內(nèi)容,如果是多選,則呈現(xiàn)出數(shù)組或者以逗號(hào)隔開(kāi)的形式
比如我選中了“喜歡”,“喜歡得不得了”,那么p.youselect中則顯示出:“喜歡,喜歡得不得了”
可以使用formArray等方式進(jìn)行,但是我在使用過(guò)程中都沒(méi)有實(shí)現(xiàn)。希望大神出手幫幫忙!
問(wèn)題解答
回答1:謝邀,基于你給的數(shù)據(jù)結(jié)構(gòu),但建議還是使用如下數(shù)據(jù)結(jié)構(gòu)(表單提交的時(shí)候,一般提交的對(duì)應(yīng)的id項(xiàng)):
[ { name: ’喜歡’, selected: true, id: 0 }, { name: ’不喜歡’, selected: false, id: 1 }]
具體可以參考 - handling-multiple-checkboxes-in-angular-forms
簡(jiǎn)單的示例代碼如下:
import { Component, OnInit } from ’@angular/core’;import { FormBuilder, FormGroup } from ’@angular/forms’;@Component({ selector: ’my-app’, template: ` <form [formGroup]='myForm'> <p *ngFor='let like of likes.controls; let i = index;' > <input type='checkbox' [formControl]='like'> {{likesArr[i]}} </p> <p class='youselect'>{{selects}}</p> </form> `,})export class AppComponent implements OnInit{ myForm: FormGroup; likesArr: string[] = [’喜歡’,’不喜歡’,’非常喜歡’,’超級(jí)喜歡’,’喜歡得不得了’]; selects: string[] = [’喜歡’]; constructor(private fb: FormBuilder) {} ngOnInit() { this.myForm = this.fb.group({ likes: this.fb.array([true, false, false, false, false]) }); this.likes.valueChanges.subscribe(values => { let selects: string[] = []; values.forEach((selected: boolean ,i: number) => {selected === true && selects.push(this.likesArr[i]) }); this.selects = selects; }); } get likes () { return this.myForm.get(’likes’); }}回答2:
個(gè)人感覺(jué)不用 Forms 好像更簡(jiǎn)單吧。。。寫(xiě)了一個(gè) Fiddle: https://jsfiddle.net/phnjg6hf/4/
HTML:
<test-component></test-component><script type='text/plain'> <p>Result: {{result()}}</p> <p *ngFor='let w of arr'><label> <input type='checkbox' value='{{w}}' [checked]='selections[w]' (change)='handle($event)' /> {{w}}</label> </p></script>
JS:
var Thing = ng.core.Component({ selector: 'test-component', template: document.getElementById('some').innerHTML})(function () { this.selections = { First: true }; this.arr = ['First', 'Second', 'Third'];});Thing.prototype.result = function () { var that = this; return this.arr.filter(function (x) {return that.selections[x]; }).join(', ');};Thing.prototype.handle = function (e) { var t = e.target, v = t.value, c = t.checked; this.selections[v] = c;};var AppModule = ng.core.NgModule({ imports: [ng.platformBrowser.BrowserModule], declarations: [Thing], bootstrap: [Thing], providers: []})(function () { });ng.platformBrowserDynamic.platformBrowserDynamic().bootstrapModule(AppModule);
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker-compose中volumes的問(wèn)題3. boot2docker無(wú)法啟動(dòng)4. nignx - docker內(nèi)nginx 80端口被占用5. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.6. docker容器呢SSH為什么連不通呢?7. node.js - antdesign怎么集合react-redux對(duì)input控件進(jìn)行初始化賦值8. dockerfile - 為什么docker容器啟動(dòng)不了?9. java - SSH框架中寫(xiě)分頁(yè)時(shí)service層中不能注入分頁(yè)類10. 關(guān)于docker下的nginx壓力測(cè)試

網(wǎng)公網(wǎng)安備