2016-11-17 60 views
0

我想建立一個基本的畫廊,不能得到的圖像顯示。如果我直接訪問另一個選項卡中的圖像,則圖像顯示的角度是正確的。如果直接訪問URL,瀏覽器可以顯示圖像,但無法使用角度。 其中一個圖像:爲什麼我的畫廊中的圖像返回403錯誤?

https://lookpic.com/O/i2/593/EHC7oQ7d.jpeg 

GalleryComponent:

import {Component, Input, ElementRef} from "@angular/core"; 
import {Http, Response} from "@angular/http"; 
import {Logger} from "angular2-logger/core"; 
import {ImageModel} from "app/core/index"; 

@Component({ 
    selector: "[app-gallery]", 
    template: 
` 
<div class="gallery--main"> 
    <img [src]="src"> 
    <div *ngIf="images.length > 1"> 
    <span class="prev" (click)="prev()"><i class="fa fa-chevron-left"></i></span> 
    <span class="next" (click)="next()"><i class="fa fa-chevron-right"></i></span> 
    </div> 
    <ul class="gallery--thumbs"> 
    <li *ngFor="let image of images"> 
     <img src="{{image.src1 || ''}}" (click)="setFocus(image)"> 
    </li> 
    </ul> 
</div> 
` 
}) 
export class GalleryComponent { 
    private _el: HTMLElement; 
    @Input() images: Array<ImageModel>; 
    src: string; 
    _hasFocus: number; 

    constructor(private _l: Logger, private el: ElementRef, private http: Http) { 
    this._el = el.nativeElement; 
    this.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="; 
    } 
    ngAfterViewInit() { 
    console.log(this.images); 
    this.src = this.images[0].src1; 
    this._hasFocus = 0; 
    } 
    setFocus(image: ImageModel): void { 
    this.src = image.src1; 
    } 
    prev(): void { 
    let previous = (this._hasFocus === 0) ? this.images.length - 1 : this._hasFocus - 1; 
    console.log(previous); 
    this.src = this.images[previous].src1; 
    this._hasFocus = previous; 
    } 
    next(): void { 
    let next = (this._hasFocus === this.images.length + 1) ? 0 : this._hasFocus + 1; 
    this.src = this.images[this._hasFocus - 1].src1; 
    this._hasFocus = next; 
    } 

} 

ImageModel:

export class ImageModel { 
    position: number; 
    src1: string; 
    src2: string; 
    src3: string; 

    constructor(position: number, url1: string, url2?: string, url3?: string) { 
    this.position = position; 
    this.src1 = url1; 
    this.src2 = url2; 
    this.src3 = url3; 
    } 
} 

和圖像嘲笑:

import {ImageModel} from "app/core/index"; 

export const MOCK_IMAGES: Array<ImageModel> = [ 
    new ImageModel(0, "https://lookpic.com/O/i2/593/EHC7oQ7d.jpeg", undefined, undefined), 
    new ImageModel(1, "https://lookpic.com/O/i2/1952/MhbYTCYy.jpeg", undefined, undefined), 
    new ImageModel(2, "https://lookpic.com/O/i2/1072/zi56gXq6.jpeg", undefined, undefined), 
    new ImageModel(3, "https://lookpic.com/O/i2/1072/pvulQlkp.jpeg", undefined, undefined), 
]; 

回答

2

HTTP狀態代碼403表示噸他服務器瞭解請求,但請求的資源被阻止。

https://en.wikipedia.org/wiki/HTTP_403

Lookpic.com已超過可能阻塞服務了它的圖片,除非請求通過瀏覽器直接進行。我想象它在打開標籤後顯示在你的Angular應用程序中,因爲請求已經單獨通過瀏覽器完成(緩存,或者請求資源的瀏覽器已經被授權這樣做,並且跨越標籤)。

+0

請嘗試http://placehold.it或http://lorempixel.com。 – isherwood

+0

我在瀏覽器中運行角2。 – emvidi

+1

但是你正在間接地(通過Angular)製作圖片請求,而不是直接使用瀏覽器。這類間接請求被阻止並不罕見,以防止網站提供不屬於它們的資源並最終消耗帶寬。 –