在AngularJS中从一个组件传递数据到另一个组件

huangapple go评论67阅读模式
英文:

Passing data from one component to another in angular js

问题

export class AppComponent {
  title = 'shopping-cart';

  ngOnInit() {

  }

  images = [
    {
      title: 'At the beach',
      url: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1173&q=80',
    },
    {
      title: 'At the forest',
      url: 'https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
    {
      title: 'At the City',
      url: 'https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
    {
      title: 'At the Snow',
      url: 'https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
    {
      title: 'At the beach',
      url: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1173&q=80',
    },
    {
      title: 'At the forest',
      url: 'https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
    {
      title: 'At the City',
      url: 'https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
    {
      title: 'At the Snow',
      url: 'https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
    },
  ];
}

<!-- App component template -->
<app-nav></app-nav>
<div class="container">
  <div class="columns mt-5 is-8 is-variable is-flex-wrap-wrap">
    <div class="column is-4-tablet is-3-desktop" *ngFor="let image of images">
      <app-card [image]="image"></app-card>
    </div>
  </div>
</div>

<!-- card component ts -->

import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-card',
  templateUrl: './card.component.html',
  styleUrls: ['./card.component.css'],
})
export class CardComponent implements OnInit {
  @Input() image = {};

  constructor() {}

  ngOnInit(): void {
    console.log(this.image);
  }
}

<!-- card component template -->

<div class="card">
  <div class="card-image">
    <figure class="image is-4by3">
      <img [src]="image.url" [alt]="image.title" />
    </figure>
  </div>
  <footer class="card-footer">
    <p class="card-footer-item">
      <span>
        View on
        <a href="https://twitter.com/codinghorror/status/506010907021828096">Twitter</a>
      </span>
    </p>
    <p class="card-footer-item">
      <span> Share on <a href="#">Facebook</a> </span>
    </p>
  </footer>
</div>

{{ image | json }}

希望这可以帮助你解决问题。

英文:
 export class AppComponent {
title = &#39;shopping-cart&#39;;
ngOnInit() {
}
images = [
{
title: &#39;At the beach&#39;,
url: &#39;https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1173&amp;q=80&#39;,
},
{
title: &#39;At the forest&#39;,
url: &#39;https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
{
title: &#39;At the City&#39;,
url: &#39;https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
{
title: &#39;At the Snow&#39;,
url: &#39;https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
{
title: &#39;At the beach&#39;,
url: &#39;https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1173&amp;q=80&#39;,
},
{
title: &#39;At the forest&#39;,
url: &#39;https://images.unsplash.com/photo-1448375240586-882707db888b?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
{
title: &#39;At the City&#39;,
url: &#39;https://images.unsplash.com/photo-1449824913935-59a10b8d2000?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
{
title: &#39;At the Snow&#39;,
url: &#39;https://images.unsplash.com/photo-1517299321609-52687d1bc55a?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1170&amp;q=80&#39;,
},
];
}
&lt;-- App component template --&gt;
&lt;app-nav&gt;&lt;/app-nav&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;columns mt-5 is-8 is-variable is-flex-wrap-wrap&quot;&gt;
&lt;div class=&quot;column is-4-tablet is-3-desktop&quot; *ngFor=&quot;let image of images&quot;&gt;
&lt;app-card [image]=&quot;image&quot;&gt;&lt;/app-card&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<-- card component ts -->

import { Component, Input, OnInit } from &#39;@angular/core&#39;;
@Component({
selector: &#39;app-card&#39;,
templateUrl: &#39;./card.component.html&#39;,
styleUrls: [&#39;./card.component.css&#39;],
})
export class CardComponent implements OnInit {
@Input() image = {};
constructor() {}
ngOnInit(): void {
console.log(this.image);
}
}

<-- card component template-->

&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-image&quot;&gt;
&lt;figure class=&quot;image is-4by3&quot;&gt;
&lt;img [src]=&quot;image.title&quot; [alt]=&quot;&quot; /&gt;
&lt;/figure&gt;
&lt;/div&gt;
&lt;footer class=&quot;card-footer&quot;&gt;
&lt;p class=&quot;card-footer-item&quot;&gt;
&lt;span&gt;
View on
&lt;a href=&quot;https://twitter.com/codinghorror/status/506010907021828096&quot;
&gt;Twitter&lt;/a
&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=&quot;card-footer-item&quot;&gt;
&lt;span&gt; Share on &lt;a href=&quot;#&quot;&gt;Facebook&lt;/a&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;/footer&gt;
&lt;/div&gt;

{{ image | json }}

I have my component as follows, i'm passing down the images to an app-card component, however when i'm trying to access the image.title property it shows that it doesn't exist, but when piping(debugging purpose) the property it shows the date for each card respectively. What am I missing please help

答案1

得分: 1

Sure, here are the translated parts:

  1. 当我定义我的输入时,它们看起来像这样:

    @Input('image') image: any = {};

    不确定是否会有差异。

  2. 在你的卡片组件中,你想使用 url,而不是 title:

    <img [src]="image.url" [alt]="" />

  3. 然后像这样添加标题:

    <div>{{image.title}}</div>

  4. 还有一个好主意是在顶部添加 *ngIf 以防止加载时出错:

    <div *ngIf="image" class="card">

英文:

couple of things.

  1. When I define my Input, they look like this:

    @Input(&#39;image&#39;) image: any = {};

not sure if that makes a difference or not.

  1. In your card component you want to use url, not title:

    &lt;img [src]=&quot;image.url&quot; [alt]=&quot;&quot; /&gt;

  2. Then add title like this

    &lt;div&gt;{{image.title}}&lt;/div&gt;

  3. Also a good idea to add *ngIf at the top to prevent errors on loading:

    &lt;div *ngIf=image&quot; class=&quot;card&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月30日 02:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定