Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 传递数组';s项作为角度模板中函数的参数_Angular_Typescript - Fatal编程技术网

Angular 传递数组';s项作为角度模板中函数的参数

Angular 传递数组';s项作为角度模板中函数的参数,angular,typescript,Angular,Typescript,在我的Angular2应用程序中,我大致有以下app.component.ts: ... export class AppComponent { // themes array readonly themes = ['assets/default-theme.css', 'assets/test-theme.css', ...]; // theme setter setTheme(href: string) { setThemeLinkHref(

在我的Angular2应用程序中,我大致有以下app.component.ts:

...
export class AppComponent {
    // themes array
    readonly themes = ['assets/default-theme.css', 'assets/test-theme.css', ...];

    // theme setter
    setTheme(href: string) {
        setThemeLinkHref(href);
        console.log('Theme has been changed to: ' + href);
    }
    ...
}
我在模板(app.component.html)中有以下代码:


如何将某个数组的项作为参数传递给函数?

在不复制代码的情况下,您可以使用
ngFor
如下所示

 <button mat-menu-item  *ngFor="let theme of themes" (click)="setTheme(theme)"> {{theme.name}}</button>

在不复制代码的情况下,您可以使用ngFor,如下所示:

 <button mat-menu-item  *ngFor="let theme of themes" (click)="setTheme(theme)"> {{theme.name}}</button>

去掉
readonly
修饰符,它似乎会起作用。

去掉
readonly
修饰符,它似乎会起作用。

这个答案并不针对OPs问题。为什么不呢?提供更好的方法总是更好的回答:当他试图设置主题时,为什么没有定义,而不是如何改进DOM。此外,你还需要详细说明你的答案。他为每个需要显示和触发操作的按钮使用一个标签。对不起,Sajeetharan,我知道如何使用*ngFor迭代项目。我更改了样本。这个答案没有针对OPs问题。为什么不呢?提供更好的方法总是更好的回答:当他试图设置主题时,为什么没有定义,而不是如何改进DOM。此外,你还需要详细说明你的答案。他为每个需要显示和触发操作的按钮使用一个标签。对不起,Sajeetharan,我知道如何使用*ngFor迭代项目。我换了样品。
 <button mat-menu-item  *ngFor="let theme of themes" (click)="setTheme(theme)"> {{theme.name}}</button>
themes = [ {'theme':'assets/default-theme.css','name':'Default', {'theme':'assets/test-theme.css','name':'Test'}];