在不使用模板或@View的情况下,可以使用角2吗?
我正在寻找类似的方法,就像您在示例中所做的那样:
角1
index.html
<div ng-controller="appcontroller">
<div ng-class="{active: isActive()}">
.....
</div>
</div>app.js
angular.module('app', []).controller('appcontroller', function(){
$scope.isActive = function(){
return true;
}
});如果可能的话,我猜它会是这样的:
角2
index.html
<app>
<div [ngclass]="{active: isActive()}">
.....
</div>
</app>app.ts
import {Component} from 'angular2/core';
@Component({
selector: 'app'
})
export class AppComponent {
isActive(){
return true;
}
}但不幸的是,我得到了错误:
... must have either 'template', 'templateUrl', or '@View' set.我不太喜欢将html放在Javascript中,所以我希望有某种解决办法或方法来做到这一点。
发布于 2016-01-29 14:49:52
实际上,您应该像这样实现AppComponent:
import {Component} from 'angular2/core';
@Component({
selector: 'app'
template: `
<div [ngClass]="{active: isActive()}">
.....
</div>
`
})
export class AppComponent {
isActive(){
return true;
}
}或者使用templateUrl属性:
import {Component} from 'angular2/core';
@Component({
selector: 'app'
templateUrl: 'component.html'
})
export class AppComponent {
isActive(){
return true;
}
}然后像这样使用组件:
<app></app>如果将一些HTML放在app标记中,这意味着要向组件提供一些可以使用ng-content包含在组件模板中的内容。下面是一个示例:Angular 2 Template Component。
希望它能帮到你,蒂埃里
https://stackoverflow.com/questions/35087266
复制相似问题