Angular 1 个指令在 angular 2 个应用程序中
Angular 1 directive inside angular 2 application
我们使用这个很棒的 Angular2 Seed 创建了一个 Angular 2 应用程序,它运行良好。所以我的问题是,如何升级这个 Angular 1 指令:
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;
以便我可以在下面的 Angular 2 组件中使用它:
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() { }
}
你们可能知道我将如何实现这一目标吗?有可能吗?因为我在研究过程中发现的很多其他文章都建议您的 "base" 应用程序应该是 Angular 1...
提前致谢。
弗朗索瓦
您需要使用 "@angular/upgrade": "2.0.0-rc.4",
升级到 angular2
Guide
Because a lot of the other articles that I have found during my
research suggests that your "base" application should be Angular 1...
如果您已经有 angular 1 个项目并且想升级到一个。 Angular2 不需要 angular1 作为基础
在angular2
中写入指令
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
}
我的第一个属性指令
突出显示我!
import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
directives: [HighlightDirective]
})
export class AppComponent { }
你不需要将 angular1 混淆成两个..
如何将您的 angular1 指令转换为 angular2 指令?
注意:不知道有没有用,看看就好
看这里的演示: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 {}
我们使用这个很棒的 Angular2 Seed 创建了一个 Angular 2 应用程序,它运行良好。所以我的问题是,如何升级这个 Angular 1 指令:
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;
以便我可以在下面的 Angular 2 组件中使用它:
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() { }
}
你们可能知道我将如何实现这一目标吗?有可能吗?因为我在研究过程中发现的很多其他文章都建议您的 "base" 应用程序应该是 Angular 1...
提前致谢。 弗朗索瓦
您需要使用 "@angular/upgrade": "2.0.0-rc.4",
升级到 angular2
Guide
Because a lot of the other articles that I have found during my research suggests that your "base" application should be Angular 1...
如果您已经有 angular 1 个项目并且想升级到一个。 Angular2 不需要 angular1 作为基础
在angular2
中写入指令import { Directive, ElementRef, Input } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
constructor(el: ElementRef) {
el.nativeElement.style.backgroundColor = 'yellow';
}
}
我的第一个属性指令
突出显示我!import { Component } from '@angular/core';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
directives: [HighlightDirective]
})
export class AppComponent { }
你不需要将 angular1 混淆成两个..
如何将您的 angular1 指令转换为 angular2 指令?
注意:不知道有没有用,看看就好
看这里的演示: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 {}