加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数据绑定 – 使用ng-model的Angular 2双向绑定不工作

发布时间:2020-12-17 08:39:06 所属栏目:安全 来源:网络整理
导读:无法绑定到“ngModel”,因为它不是“输入”元素的已知属性,并且没有具有对应属性的匹配指令 注意:im使用alpha.31 import { Component,View,bootstrap } from 'angular2/angular2'@Component({ selector: 'data-bind'})@View({ template:` input id="name"
无法绑定到“ngModel”,因为它不是“输入”元素的已知属性,并且没有具有对应属性的匹配指令

注意:im使用alpha.31

import { Component,View,bootstrap } from 'angular2/angular2'

@Component({
    selector: 'data-bind'
})
@View({
    template:`
        <input id="name" type="text" 
            [ng-model]="name" 
            (ng-model)="name = $event" />
        {{ name }}
    ` 
})
class DataBinding { 
    name: string;

    constructor(){
        this.name = 'Jose';
    }
}

bootstrap(DataBinding);
Angular已经在9月15日发布了其最终版本。与Angular 1不同,您可以在Angular 2中使用ngModel指令进行双向数据绑定,但是您需要以[(ngModel)](Banana在框语法中)的方式编写它。几乎所有angular2核心指令不支持kebab-case现在,而应该使用camelCase。

Now ngModel directive belongs to FormsModule,that’s why you should import the FormsModule from @angular/forms module inside imports metadata option of AppModule(NgModule). Thereafter you can use ngModel directive inside on your page.

app / app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',template: `<h1>My First Angular 2 App</h1>
    <input type="text" [(ngModel)]="myModel"/>
    {{myModel}}
  `
})
export class AppComponent { 
  myModel: any;
}

app / app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent }   from './app.component';

@NgModule({
  imports:      [ BrowserModule,FormsModule ],//< added FormsModule here
  declarations: [ AppComponent ],bootstrap:    [ AppComponent ]
})

export class AppModule { }

app / main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

Demo Plunkr

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读