2016-11-16 18 views
2

サービスにViewContainerRefを注入しようとしましたが、エラーNo provider for ViewContainerRef!を受け取りました。私はthis PRを見つけました。これは、私が後にしていることを正確に示しており、マージされたようです。ViewContainerRefをサービスに挿入するにはどうすればよいですか?

ここでは、thisを使用してプレースホルダを使用してこれを実現する方法について認識しています。

+1

'ViewContainerRef'現在のコンポーネントに属します。サービスは、現在のコンポーネントのインジェクタまたは親インジェクタに属することができるシングルトンです。親コンポーネントによってインスタンス化され、子コンポーネントに注入された場合、ビューコンテナの参照が間違っていました。これはあまり意味がありません。回避策が既に分かっている場合は、なぜこの質問をしますか? – estus

+0

どこから来ているのか分かります。この最初のアプローチが達成されたように見えたので、私はこの正確な理由でPRについて言及しました。 – onetwothree

+0

私はPRがこれに対処する方法がわからない、なぜならそれは同じデザイン問題にぶつかるからだ。そして、私は 'this.modalService.viewContainerRef = this.viewContainerRef'回避策が同じ理由で良い選択だとは思わない。サービスがシングルトンであることを考慮すると、これは間違いなくDIを通じて管理すべきである。あなたの事は何ですか? – estus

答えて

-1

ジャスト@Host()デコレータを持つコンストラクタにそれを渡すには、そのよう:

@Injectable() 
export class SomeService { 

    constructor(@SkipSelf() @Host() private viewContainerRef: ViewContainerRef) { } 

} 

注:@SkipSelf()注釈オプションですが、依存関係の解決は親インジェクタから始まる保証します。

+0

(OPでない)これは私のために働いていない、同じエラー。 – Cyral

+0

どちらも動作しませんでした – Bhail

0

それは

ContainerRefについてERRORエラー文句を保つように、この作業を取得するために苦労:キャッチされない(約束で):エラー:ViewContainerRefなしプロバイダーを! エラー:ViewContainerRefのプロバイダがありません!

import { Injectable, ViewContainerRef } from '@angular/core'; 
import { Route, CanActivate, CanLoad } from '@angular/router'; 
import { AuthenticationService } from "../../services"; 

import { Host, SkipSelf } from '@angular/core'; 
import { ModalDialogService, ModalDialogOptions } from "nativescript-angular/modal-dialog"; 

import { LoginControlComponent } from "../../controls/login/login.control.component" 

// import { TNSFancyAlert, TNSFancyAlertButton } from 'nativescript-fancyalert'; 

@Injectable() 
export class AuthGuard implements CanActivate, CanLoad { 

constructor(

    private authenticationService: AuthenticationService, 
    private modalService: ModalDialogService, 
    @SkipSelf() @Host() private viewContainerRef: ViewContainerRef 
    // private router: Router 
) { 

} 

canActivate(): Promise<boolean> { 

    console.log("Auth Guard Checked !!"); 

    return new Promise((resolve, reject) => { 
     if (this._isAuth()) { 
      resolve(true); 
     } else { 


      // use generic selection modal 
      let options: ModalDialogOptions = { 
       context: { 
        title: "Login", 
        desc: "Please Enter you Username/email and password" 

       }, 
       fullscreen: false, 
       viewContainerRef: this.viewContainerRef 
      }; 

      this.modalService.showModal(LoginControlComponent, options) 
      .then(x=>{ 

      }) 

     } 
    }); 
} 
// canActivate(): Promise<boolean> { 

//  console.log("Auth Guard Checked !!"); 

//  return new Promise((resolve, reject) => { 
//   if (this._isAuth()) { 
//    resolve(true); 
//   } else { 
//    // login sequence to continue prompting unless canceled 
//    let promptSequence = (usernameAttempt?: string) => { 
//     this.authenticationService.promptLogin('Please enter your username and password', usernameAttempt) 
//      .then(() => { 
//       console.log("User Verified"); 
//       resolve(true); 
//      }, 
//      (usernameAttempt) => { 
//       if (usernameAttempt === false) { 
//        // user canceled prompt 
//        console.log("User Cancelled"); 
//        resolve(false); 
//       } else { 
//        // initiate sequence again 
//        console.log("INI Seq. again"); 
//        promptSequence(usernameAttempt); 
//       } 
//      }); 
//    }; 
//    // start login prompt sequence 
//    // require auth before activating 
//    promptSequence(); 
//   } 
//  }); 
// } 

canLoad(route: Route): Promise<boolean> { 
    return this.canActivate(); 
} 


private _isAuth(): boolean { 
    console.log("Authencticated Value ", this.authenticationService.authenticated$.getValue()); 
    return this.authenticationService.authenticated$.getValue(); 
} 

}

関連する問題