Angular2使用HTML中的组件初始化类

时间:2017-04-21 02:25:27

标签: html function angular

我想知道以下是否可行。我可以在HTML中使用组件的初始化类来调用组件中不存在的函数吗?例如:

@Component
...
export class TestComponent implements OnInit {
   simpleHandler:SimpleHandler = new SimpleHandler();
...

然后在HTML调用中它就像这样?

<div>
{{simpleHandler.isValid()}}
</div>

或者它只能调用组件本身的函数和属性吗?

当我尝试这样做时,它会出现此错误:

TypeError: self.parentView.parentView.context.simpleHandler.isValid is not a function

1 个答案:

答案 0 :(得分:0)

是的,你可以做到。例如,ng2-file-upload

使用此模式
@Component({
  selector: 'simple-demo',
  templateUrl: './simple-demo.html'
 })
 export class SimpleDemoComponent {
   public uploader:FileUploader = new FileUploader({url: URL});
   ...
 }

并在html中:

<button type="button" class="btn btn-success btn-xs"
    (click)="item.upload()" [disabled]="item.isReady || item.isUploading || item.isSuccess">
    <span class="glyphicon glyphicon-upload"></span> Upload
</button>