简体   繁体   English

如何在有角项目的html组件之间传递字符串变量作为参数?

[英]how can I pass a string variable as argument between the html components of an angular project?

I'm making an Angular app that looks up movies on an online API, which I can add to my own database, or delete them from my database. 我正在制作一个Angular应用程序,该应用程序可以在在线API上查找电影,可以将其添加到自己的数据库中,也可以从数据库中删除它们。 There are 3 components: - movie, which has the template of a movie card (consisting of poster + title) to be shown in the other 2 components - movies: a list of all the movies added in my collection. 有3个组成部分:-电影,具有要在其他2个组成部分中显示的电影卡模板(由海报+标题组成)-电影:我收藏中所有电影的列表。 Here you can either click on the poster to go to the page with details about the movie, or delete it from my database by clicking on the fab-button. 在这里,您可以单击海报以转到包含电影详细信息的页面,也可以通过单击fab按钮将其从我的数据库中删除。 - movie-add: a simple form where you can look up movies by title or keyword in the online API. -电影添加:一种简单的形式,您可以在其中通过在线API中的标题或关键字查找电影。 Here too, clicking on the poster brings you to the details page, but the fab button adds it to my database. 在这里,单击海报也会将您带到详细信息页面,但是fab按钮会将其添加到我的数据库中。

here's the HTML code for the movie component: 这是电影组件的HTML代码:

 <div class="card green lighten-5"> <div class="card-image"> <a routerLink="/movies/movie/{{movie.id}}"> <img src={{movie.poster}} alt={{movie.title}}> </a> <a class="btn-floating halfway-fab waves-effect waves-light red" (click)="onClick(movie)"><i class="material-icons">delete</i></a> </div> <div class="card-content"> <p><b>{{movie.title}}</b></p> </div> </div> 

This is my movies: 这是我的电影:

 <h2>Movies</h2> <div class="row"> <div class="col s6 m3" *ngFor="let movie of movies"> <media-movie [movie]='movie' (selected)='deleteMovie(movie)'></media-movie> </div> </div> 

movie-add (I omitted the form since it's not relevant to my question): movie-add(我省略了表格,因为它与我的问题无关):

 <div class="row"> <div class="col s6 m3" *ngFor="let movie of movies"> <media-movie [movie]='movie' (selected)='addMovie(movie)'></media-movie> </div> </div> 

now, it does work, but I want the fab button to be red with the delete icon in the first case, and green with an add icon in the second. 现在,它确实可以工作,但是我希望fab按钮在第一种情况下为红色,带有删除图标,在第二种情况下,绿色为带有添加图标。 Is there a simple way I can add these 2 values in the tag? 有没有一种简单的方法可以在代码中添加这两个值?

I've tried this, but it doesn't work: movie: 我已经尝试过了,但是没有用:电影:

 <a class="btn-floating halfway-fab waves-effect waves-light {{color}}" (click)="onClick(movie)"><i class="material-icons">{{iconAction}}</i></a> 

movie-add: 电影加:

 <media-movie [movie]='movie' [color]=green [iconAction]='add' (selected)='addMovie(movie)'></media-movie> 

I agree that your question is a bit unclear, but I took a stab at an answer for you. 我同意您的问题尚不清楚,但我为您回答一个问题。 There are a few ways you can do conditional binding in angular. 您可以通过几种方法进行有条件的角度绑定。 If you want to conditionally add a class you can use ngClass . 如果要有条件地添加一个类,可以使用ngClass If you want to conditionally bind styles you can use property binding on the style like this [style.font-size.px]="isImportant ? '30' : '16'" , or you can user ngStyle . 如果要有条件地绑定样式,则可以对这种样式使用属性绑定,例如[style.font-size.px]="isImportant ? '30' : '16'" ,或者可以使用ngStyle

I've built a quick sample app using ngStyle. 我使用ngStyle构建了一个快速的示例应用程序。 It has a test button and the background color changes based on clicking the toggle file. 它具有一个测试按钮,并且通过单击切换文件来更改背景颜色。 Let me know if this helps or if you can refine your answer a bit more I'd be happy to tweak my example. 让我知道这是否有帮助,或者您是否可以进一步完善答案,我很乐意调整示例。

https://stackblitz.com/edit/angular-312aft https://stackblitz.com/edit/angular-312aft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM