2016-05-21 71 views
1

我無法獲得組件屬性的插值。如果我輸入{{1+1}},它會,但不會。我試圖簡單地顯示{{title}},並在兩個地方(app和NutriForm組件)中聲明它,但是沒有任何效果。Angular2插值不起作用

這些是相關文件。

app.component.ts

import { Component } from '@angular/core'; 
import {NutriFromComponent} from './NutriForm.component' 

@Component({ 
    selector: 'my-app', 
    directives: [NutriFromComponent], 
    template: '<NutriForm></NutriForm>' 
}) 
export class AppComponent { 
    title = "hello"; 
} 

plain.html

<html> 
    <head> 
     <title>Angular 2 QuickStart</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <link href="../public/stylesheets/style.css" rel="stylesheet" /> 
     <!-- 1. Load libraries --> 
     <!-- Polyfill(s) for older browsers --> 
     <script src="node_modules/core-js/client/shim.min.js"></script> 
     <script src="node_modules/zone.js/dist/zone.js"></script> 
     <script src="node_modules/reflect-metadata/Reflect.js"></script> 
     <script src="node_modules/systemjs/dist/system.src.js"></script> 
     <!-- 2. Configure SystemJS --> 
     <script src="systemjs.config.js"></script> 
     <script> 
      System.import('app').catch(function(err){ console.error(err); }); 
     </script> 
    </head> 
    <!-- 3. Display the application --> 
    <body> 
     <my-app>Loading...</my-app> 
    </body> 
</html> 

NutriForm.component.ts

import {Component} from '@angular/core'; 
import {Control , FormBuilder , Validators , NgForm} from '@angular/common'; 
import {ControlGroup} from '@angular/common'; 
import {NutriModel} from '../Models/NutriModel.ts'; 

@Component({ 
    selector: 'NutriForm', 
    templateUrl: 'views/NutriForm.component.html' 
}) 
export class NutriFromComponent { 

    title: string; 

    constructor() { 
     this.title = 'Tour of Heroes'; 
    } 
} 

NutriForm.component.html

<h2> {{title}}</h2> 

<table> 
    <tr> 
     <td></td> 
     <td>Your Value</td> 
     <td>Recommended</td> 
     <td>Average</td> 
    </tr> 
    <tr> 
     <!--<td><input type="text" required [(ngModel)]="model.sex" ngControl="sex" /></td>--> 
     <td>Your Value</td> 
     <td>Recommended</td> 
     <td>Average</td> 
    </tr> 

</table> 

<button type="submit" class="btn btn-default">Submit</button> 
+3

代碼看起來不錯。把它放在一個笨重的地方作進一步調查,否則沒有人會知道什麼是錯的。 Plunker示例:https://angular.io/resources/live-examples/toh-1/ts/plnkr.html – tibbus

+0

{{title}}的輸出? –

回答

1

相反

export class NutriFromComponent { 

    title: string; 

    constructor() { 
     this.title = 'Tour of Heroes'; 
    } 
} 

應用

export class NutriFromComponent { 

    title = 'Tour of Heroes'; 

} 

它應該工作