简体   繁体   English

Angular2服务呈现HTML

[英]Angular2 Service render HTML

Working through an Angular2 tutorial. 完成Angular2教程。 The challenge was to create a 'tweet' component that used a 'tweet' service to retrieve its posts. 挑战在于创建一个“推文”组件,该组件使用“推文”服务来检索其帖子。

The service is to return an array of hard-coded tweets. 该服务将返回一组硬编码的推文。 This is just for the sake of exercise; 这只是为了锻炼; obviously not efficiency. 显然效率不高。 However, the service can only return an array of strings so I am not sure how to return an array of HTML that can then be rendered as HTML rather than text in the view. 但是,该服务只能返回一个字符串数组,所以我不知道如何返回一个HTML数组,然后可以将其呈现为HTML而不是视图中的文本。 I've read up and seen that there are of course better ways of accomplishing this than through a service, perhaps through a directive. 我已经阅读并看到当然有更好的方法来实现这一点,而不是通过服务,也许通过指令。 However this is what the challenge is. 然而,这就是挑战。

This is what I've come up with so far. 这是我到目前为止所提出的。 The view renders the array of tweets from the service as text. 该视图将来自服务的推文数组呈现为文本。 Everything else works fine as I tested it with the service returning an array of strings which rendered as planned. 其他所有工作正常,因为我测试它返回一个按计划呈现的字符串数组的服务。

app.component.ts: app.component.ts:

import {Component} from 'angular2/core';

import {TweetComponent} from './tweet.component'

@Component({
    selector: 'my-app',
    template: `
        <tweet></tweet>
    `,
    directives: [TweetComponent]
})
export class AppComponent { 

}

tweet.component.ts: tweet.component.ts:

import {Component} from 'angular2/core'
import {TweetService} from './tweet.service'
@Component ({
    selector: 'tweet',
    template: `
    <li *ngFor="#tweet of tweets">
        {{tweet}}
    </li>

    `,

    providers: [TweetService]
})

export class TweetComponent {
    tweets;
    constructor(tweetService: TweetService){
    this.tweets = tweetService.getTweets();
} 
}

tweet.service.ts: tweet.service.ts:

export class TweetService {
    getTweets() {
        return [`
        <div class="media">
         <div class="media-left">
        <a href="#">
          <img class="media-object" src="http://lorempixel.com/100/100/people/?1" alt="...">

        </a>
        </div>
      <div class="media-body">
        <h4 class="media-heading">Tweet 1<br>Media Title 1?<br><like></like></h4>
      </div>
    </div>
       `,
        `
        <div class="media">
         <div class="media-left">
        <a href="#">
          <img class="media-object" src="http://lorempixel.com/100/100/people/?2" alt="...">

        </a>
        </div>
      <div class="media-body">
        <h4 class="media-heading">Tweet 2<br>Media Title 2<br><like></like></h4>
      </div>
    </div>`
        ];
    }
    }

In tweet.component.ts use innerHTML: 在tweet.component.ts中使用innerHTML:

import {Component} from 'angular2/core'
import {TweetService} from './tweet.service'
@Component ({
    selector: 'tweet',
    template: `
    <li *ngFor="#tweet of tweets">
        <span [innerHTML]="tweet"></span>
    </li>

    `,
    providers: [TweetService]
})

by the way, if you are using more recent angular2 version you should change 顺便说一句,如果你使用更新的angular2版本,你应该改变

<li *ngFor="#tweet of tweets">

to

<li *ngFor="let tweet of tweets">

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

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