如何将input标签的ngModel值转换为angularjs4中的组件?

时间:2017-09-22 07:22:35

标签: angular typescript components angular-cli

我是angularjs4的新手。我正在处理angular-cli.Here我需要在我的组件中获取输入标签的ngModel值。如何在输入字段中输入该值?通过使用该值,我需要编写一个用于显示搜索的过滤器我的页面上的数据。如何在angular4中实现这个? 这是我的app.component.html和app.component.ts文件:

import {
    Component
} from '@angular/core';
import {
    Http,
    Response,
    Headers,
    RequestOptions
} from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    productsList = '';
    show: boolean;
    hide: boolean;
    listBtn: boolean;
    gridBtn: boolean;
    values = '';

    onKey(event: any) { // without type info
        this.values += event.target.value;
        console.log("value " + this.values);
    }
    listView() {
        this.gridBtn = true;
        this.show = true;
        this.hide = false;
        this.listBtn = false;
    }
    gridView() {
        this.listBtn = true;
        this.gridBtn = false;
        this.show = false;
        this.hide = true;

    }
    constructor(private http: Http) {
        this.show = false;
        this.hide = true;
        this.show = false;
        this.listBtn = true;
        this.gridBtn = false;
        this.getData();
    }
    createAuthorizationHeader(headers: Headers) {
        headers.append('Authorization', 'Basic ' +
            btoa('ck_543700d9f8c08268d75d3efefb302df4fad70a8f:cs_f1514261bbe154d662eb5053880d40518367c901'));
        headers.append("Content-Type", "application/x-www-form-urlencoded");
    }
    getData() {
        console.log('hellooo');
        let headers = new Headers();
        this.createAuthorizationHeader(headers);
        return this.http.get(' https://www.colourssoftware.com/wordpress/wp-json/wc/v2/products', {
            headers: headers
        })
            .subscribe(res => {
                const products = res.json();
                console.log(products);
                this.productsList = products;
                console.log(this.productsList);
            })


    }

}

HTML

<div class="container" align="center">
    <div class="row">
        <div class="col-sm-6 col-sm-offset-3">
            <div class="input-group stylish-input-group">
                <input type="text" class="form-control" placeholder="Let's find your product....." (keyup)="onKey($event)">
                <span class="input-group-addon">
                    <button type="submit">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                </span>
            </div>
        </div>
    </div>
    <br>
</div>


<br>
<div *ngIf="show">
    <ul class="list-group">
        <li class="list-group-item" *ngFor="let data of productsList">
            <img src="{{data.images[0].src}}" alt="image" width="auto" height="200px">
            <span>{{data.name}}</span>
            <span>{{data.regular_price}}</span>
        </li>
    </ul>
</div>

5 个答案:

答案 0 :(得分:2)

1.如果您想要单向数据绑定(组件到HTML),请使用 -

<input [ngModel]="variable">

2.如果您想要双向数据绑定(组件到HTML,反之亦然),请使用 -

<input [(ngModel)]="variable">

3.如果您想在使用单向数据绑定时使用html中输入字段中更改的数据,则必须使用(ngModelChange),如下所示:

<input [ngModel]="variable"(ngModelChange)="function_to_fire_on_model_change($event)">

答案 1 :(得分:1)

<强>更新

如果你想得到输入值,但是没有ngModel(就像在你的snipet中你没有使用它),你可以这样:

 <input type="text" #input class="form-control" placeholder="Let's find your product....." (keyup)="onKey($event, input.value)">

onKey(event, newValue){
  console.log(newValue);
  console.log(event.key)
}

<小时/> 通常,模式为:

<强> HTML

<input [(ngModel)]="yourModel" ....> 

<input [ngModel]="yourModel" (ngModelChange)="doSomething($event)"....> 

<强>打字稿:

yourModel:any;
....
doSomething(event){
   console.log(event) // input value is logged
}

此处输入中的任何更改都将更新ngModel,因为它是双向绑定。

答案 2 :(得分:0)

<input [(ngModel)]="name"> // two way data binding

<input [(ngModel)]="name" (ngModelChange)="change()">  // two way data binding with onchange property

<input [ngModel]="name"> // one way data binding

在TS

name: any

点击此处查看示例https://stackblitz.com/edit/angular-pmatzc

答案 3 :(得分:0)

您可以在模板中使用以下行

<input type="text" [(ngModel)]="username" name="username" class="form-control" placeholder="username"  >

,在component.ts文件中,您可以通过this.username

来访问元素

答案 4 :(得分:0)

如果使用 ngForm,则所有具有 [(ngModel)]="" 的输入字段都必须有一个带值的属性名称:

<input [(ngModel)]="firstname" name="something">