title: Getting dependency from Injector manually inside a directive
body: <p>I am trying to create a generic directive which will take a class type for rule validation and according to the rule in the class the directive will either show or hide an element. </p>
  
  <p>This is my attempt so far.</p>
  
  <p><a href="http://plnkr.co/edit/GyDEUicFVVNt1AxywU2R?p=info" rel="nofollow noreferrer">PLUNKER</a> Demo</p>
  
  <p><strong>myIf-Directive.ts</strong></p>
  
  <pre><code>@Directive({
    selector: '[myIf]'
  })
  export class MyIfDirective {
    constructor(private _viewContainer: ViewContainerRef,
      private _template: TemplateRef&lt;Object&gt;) 
    { }
  
    @Input() set myIf(rule: string) {
      //rule class type will come as string
      //how can I use string token to get dependency from injector?
      //currently harcoded
      //will the injector create new instance or pass on instance from parent?
      let injector = ReflectiveInjector.resolveAndCreate([AdminOnly]);
      let adminOnly : IRule = injector.get(AdminOnly);
      let show = adminOnly.shouldShowElement();
      show ? this.showItem() : this.hideItem();
    }
    private showItem() {
      this._viewContainer.createEmbeddedView(this._template);
    }
  
    private hideItem() {
      this._viewContainer.clear();
    }
  }
  </code></pre>
  
  <p><br/>
  <strong>app-component.ts</strong></p>
  
  <pre><code>@Component({
    selector: 'my-app',
    template: `
      &lt;div *myIf="'AdminOnly'"&gt;
        &lt;h2&gt;Hello {{name}}&lt;/h2&gt;
      &lt;/div&gt;
    `,
  })
  export class App {
    name:string;
    constructor() {
      this.name = 'Angular2'
    }
  }
  </code></pre>
  
  <p>But I am stuck in 2 places:</p>
  
  <ol>
  <li>I keep getting the error <code>No Provider for AuthService</code></li>
  <li>I do not know how I can get the dependency from Injector using class name as string rather than the type</li>
  </ol>
  
  <p>Any suggestion whether this is the right way to do it or where I am going wrong is highly appreciated.</p>