Angular2の内挿(interpolation)、2Wayバインディングの概要についてまとめました。
※Angular2の2.0.0版、TypeScriptを使って確認したものです。(デモはv4.4.3、v2.0.0で動作確認)
※目次をクリックすると目次の下部にコンテンツが表示されます。
内挿(interpolation)
・内挿(interpolation)の設定をすると、スクリプト内のデータモデルに設定した変数の値がHTML内の関連付けられた要素に即座に反映されます。
・内挿(interpolation)を行うには、二重波括弧{{…}}を使用を使用します。
※二重波括弧の文字を表示したい場合は?
Angularでは、二重波括弧は内挿(interpolation)として解釈されてしまうため、そのままでは文字として表示する事が出来ません。
表示する場合は、下記のようにngNonBindableを使います。
<div ngNonBindable>二重波括弧{{…..}}を表示するには、ngNonBindableを使用。</div>
・内挿(interpolation)を行うには、二重波括弧{{…}}を使用を使用します。
※二重波括弧の文字を表示したい場合は?
Angularでは、二重波括弧は内挿(interpolation)として解釈されてしまうため、そのままでは文字として表示する事が出来ません。
表示する場合は、下記のようにngNonBindableを使います。
<div ngNonBindable>二重波括弧{{…..}}を表示するには、ngNonBindableを使用。</div>
2Wayデータバインディング
・内挿(interpolation)では、スクリプトからHTMLの要素へ、という方向でしたが、HTMLの要素からスクリプトへ反映させ、双方向に同期させることも出来ます。
・この場合は、ngModelディレクティブを使用します。ユーザーがデータを入力できるHTML要素と一緒に使用する必要があるので、input、texterea、select要素などの属性に指定して使用します。
●使用例
下記例では、nameを2Wayデータバインディングとして設定してあるので、ユーザーが入力した値が即座に上の行の「私の名前は・・」の部分に表示されます。
(スクリプト)
public name = “初期値”;
(HTML)
<div>私の名前は{{name}}です。</div>
名前:<input [(ngModel)]=”name” placeholder=”name” />
・この場合は、ngModelディレクティブを使用します。ユーザーがデータを入力できるHTML要素と一緒に使用する必要があるので、input、texterea、select要素などの属性に指定して使用します。
●使用例
下記例では、nameを2Wayデータバインディングとして設定してあるので、ユーザーが入力した値が即座に上の行の「私の名前は・・」の部分に表示されます。
(スクリプト)
public name = “初期値”;
(HTML)
<div>私の名前は{{name}}です。</div>
名前:<input [(ngModel)]=”name” placeholder=”name” />
デモ
Angular : 4.4.3(2.0.0でも動作確認済み)
Bootstrap : 3.x
Bootstrap : 3.x
Loading…
<html>
<head>
<title>Angular 2 Demo</title>
<script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://npmcdn.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="./js/systemjs.config.js"></script>
<script>
System.import('./ts/app.ts').catch(function(err){ console.error(err); }); </script>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="container">
<my-app>Loading...</my-app>
</div>
</body>
</html>
(systemjs.config.js)
(function (global) {
var ngVer = '@4.4.3';
System.config({
transpiler: 'ts',
typescriptOptions: {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"strict": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'app': 'app',
'@angular/animations': 'npm:@angular/animations' + ngVer + '/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations' + ngVer + '/bundles/animations-browser.umd.js',
'@angular/core': 'npm:@angular/core' + ngVer + '/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common' + ngVer + '/bundles/common.umd.js',
'@angular/common/http': 'npm:@angular/common' + ngVer + '/bundles/common-http.umd.js',
'@angular/compiler': 'npm:@angular/compiler' + ngVer + '/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser' + ngVer + '/bundles/platform-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser' + ngVer + '/bundles/platform-browser-animations.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + ngVer + '/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http' + ngVer + '/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router' + ngVer + '/bundles/router.umd.js',
'@angular/router/upgrade': 'npm:@angular/router' + ngVer + '/bundles/router-upgrade.umd.js',
'@angular/forms': 'npm:@angular/forms' + ngVer + '/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade' + ngVer + '/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade' + ngVer + '/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs@5.0.1',
'tslib': 'npm:tslib/tslib.js',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.3.2/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts',
meta: {
'./*.ts': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template:`
<strong>1)内挿(interpolation)</strong>
<div>二重波括弧:私は{{country}}在住の{{gender}}です。</div>
<div ngNonBindable>二重波括弧{{.....}}を表示するには、ngNonBindableを使用。</div
>
<br />
<strong>2)2Wayデータバインディング</strong>
<div>私の名前は{{name}}です。</div>
名前: <input [(ngModel)]="name" placeholder="name">
`
})
export class AppComponent {
public country = "日本";
public gender = "男性";
public name = "初期値";
}
@NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);