angular - 如何在 Angular 中创建一个简单的主机组件?

标签 angular typescript

我是 Angular 新手,我正在阅读 https://angular.io/guide/template-syntax#interpolation-

enter image description here

<!-- "The sum of 1 + 1 is not 4" -->
<p>The sum of 1 + 1 is not {{1 + 1 + getVal()}}.</p>

我有组件product-list

enter image description here

文件product-list.component.html

<h2>Products</h2>

<div *ngFor="let product of products">
  <h3><a [title]="product.name + ' details'">{{product.name}}</a></h3>
  <p *ngIf="product.description">Description: {{product.description}}</p>
  <button (click)="share()">Share</button>
  <p>The sum of 1 + 1 is not {{1 + 1 + getVal()}}.</p>
</div>

文件product-list.component.ts

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

import {products} from '../products';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent {
  products = products;

  share() {
    window.alert('The product has been shared!');
  }
}

如何创建getVal()主机组件?

最佳答案

只需在组件中声明该方法

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

import {products} from '../products';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent {
  products = products;

  share() {
    window.alert('The product has been shared!');
  }

  getVal() {
     // DO YOUR WORK
  }
}

关于angular - 如何在 Angular 中创建一个简单的主机组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58870507/

相关文章:

angular - 如何从 Angular - ng2 文件上传打开输入类型 ="file"对话框

javascript - 如何使用 typescript 导入 javascript 中导出的 const

javascript - 有没有办法在 typescript 类中生成 getter 和 setter 对?

javascript - Rollup + Typescript + ESLint 错误警告

Angular Material 日期选择器最小和最大日期验证消息

一个间隔内的Angular2 http请求

angular - 模型更改时 View 未更新

angular - 在 Angular 4 中保存全局设置的位置

javascript - 为什么参数 'props' 隐式具有 'any' 类型?

javascript - MomentJs 给出了错误的月份