0

We created an Angular 2 application using this awesome Angular2 Seed which works very well. So the question that I have is, how can I upgrade this Angular 1 directive:

import template from './sbgOutlineButton.html!text';

var app = angular.module('sbgOutlineButton', []);

app.directive('sbgOutlineButton', function() {
    let link = function(scope, element, attributes) {
        if (attributes.icon === undefined) {
            let materialIcon = element.find('i.material-icons');
            materialIcon.addClass('hidden');
        }

    };

    return {
        link: link,
        restrict: 'E',
        template: template,
        replace: true,
        transclude: true,
        scope: { icon: '@' }
    };
});

export default app;

So that I can use it in the following Angular 2 component:

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { UpgradeAdapter } from '@angular/upgrade';

@Component({
    moduleId: module.id,
    selector: 'test-page',
    templateUrl: 'testpage.page.html',
    styleUrls: ['testpage.page.css']
})
export class TestPage implements OnInit {
    constructor() { }
    ngOnInit() { }
}

Do you guys maybe have any idea on how I will be able to accomplish this? Is it even possible? Because a lot of the other articles that I have found during my research suggests that your "base" application should be Angular 1...

Thanks in advance. Francois

UberSwyser
  • 69
  • 2
  • 7
  • I realize this is a bit old and things have changed, but why is the answer "rewrite it in v2"? I have tried using UpgradeComponent but even using the exact code that is documented in the code doesn't seem to work, yet everywhere I look no one seems to know how or why it doesn't work. Perhaps it is still just too early days and there is no reliable method for using v1 directives in v2? – Tim Hobbs Jul 13 '17 at 08:03

2 Answers2

0

How about converting your angular1 directive to angular2 directive?

NOTE: I don't know whether it will be useful or not but just have a look.

Look at the demo here : https://plnkr.co/edit/4Fhtm76iJl0aQmgjO7n0?p=preview

customeDirective.ts

import {Directive, Attribute,ElementRef,Renderer} from '@angular/core';

@Directive({
  selector: '[myAttr]'
})

export class myDir {
    constructor(@Attribute('icon') private icon,private el:ElementRef,private rd: Renderer){
    console.log(this.icon);
    if(this.icon===null){    //<--- you can play here as per your need.
      console.log('icon is undefined');
    }
    else{
            rd.setElementClass(el.nativeElement, 'myClass',true);
    }
    console.log(el.nativeElement); 
  }
}

AppComponent.ts

//our root app component
import {Component} from '@angular/core';
import {myDir} from 'src/customDirective';

@Component({
  selector: 'my-app',
  directives:[myDir],
  template: 
  `
  <style>
    .myClass{
      color:red;
      background:yellow;
    }
  </style>


    <div myAttr icon="myIcon">Angular2</div>  <!-- icon attribute is present so it will add the class -->

            <!-- OR USE BELOW HTML INSTEAD OF ABOVE -->

    <div myAttr>Angular2</div>                 <!-- icon attribute is not present so it gives null -->


  `
})
export class App {}
micronyks
  • 54,797
  • 15
  • 112
  • 146
  • Converting it would be the best answer, except we have a codebase with a massive amount of directives that have been made reusable. – UberSwyser Aug 05 '16 at 12:27
-5

You need to upgrade to angular2 by using "@angular/upgrade": "2.0.0-rc.4", Guide

Because a lot of the other articles that I have found during my research suggests that your "base" application should be Angular 1...

It's if you have a already angular 1 project and you want to upgrade to one. Angular2 don't need angular1 as base

writing directive in angular2

import { Directive, ElementRef, Input } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }
}

My First Attribute Directive

Highlight me!
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
  selector: 'my-app',
  templateUrl: 'app/app.component.html',
  directives: [HighlightDirective]
})
export class AppComponent { }

you Don't need to mix up angular1 into two..

Jorawar Singh
  • 7,463
  • 4
  • 26
  • 39
  • What do I need to upgrade to Angular2? The Angular 1 directive? – UberSwyser Aug 05 '16 at 12:15
  • as i can see you already working with angular2 and i guess you are learning. you can write same directive in angular2 instead if angular1. Upgrading is not easy and you do upgrade if you have a large app and you don't want to rewrite your code, [take a look into angular2 start app](https://github.com/blinfo/angular2-webpack-seed) – Jorawar Singh Aug 05 '16 at 12:18
  • We unfortunately have to upgrade seeing that we have a massive repo of already developed angular 1 directives. Rewriting them all is just not feasible. – UberSwyser Aug 08 '16 at 09:45
  • the you should go with the upgrade and hope that you have followed the angular style guide for not getting conflicts :-) and if answer has gave you right information please accept it :-) – Jorawar Singh Aug 08 '16 at 09:53
  • Yea, so what I am trying to figure out is how to do the upgrade... I know how to put an Angular 2 component in Angular 1 but not the other way around... – UberSwyser Aug 10 '16 at 06:13
  • 2
    Why is this the accepted answer? It's basically telling you to re-write your directive, with links to easily-available content and a copy/paste from the docs... – Chris McCall Oct 22 '16 at 19:07