【发布时间】:2016-12-29 01:56:45
【问题描述】:
我正在尝试将我的 Angular 1 指令迁移到 Angular 2。这是我的简单 Angular 1 指令
extCheckbox.html
<input type=checkbox
name ="{{checkboxName}}"
id="{{checkboxId}}"
data-ng-readonly="isReadonly"
data-ng-checked="isChecked"/>
这是我的指令 extCheckbox.js
var app = angular.module('Demo');
app.directive('extCheckbox', [function () {
return {
restrict: 'E',
scope: {
label: '@',
name: '@',
id: '@',
isDisabled: '=?',
isReadonly: '=?',
isChecked: '=?',
},
templateUrl: './extCheckbox.html',
replace: true,
transclude: false,
link: function (scope, element, attrs) {
if (attrs.isChecked === undefined) {
scope.isChecked =true;
}
}
};
}]);
我将其迁移到 Angular 2,如下所示,
extCheckbox.Component.ts
import {Component} from '@angular/core';
import {UpgradeAdapter} from '@angular/upgrade';
const upgradeAdapter = new UpgradeAdapter();
let ExtCheckbox = upgradeAdapter.upgradeNg1Component('ext-Checkbox');
@Component({
selector: 'ext-mycheckbox',
template: '<ext-checkbox></ext-checkbox>',
directives: [ExtCheckbox]
})
export class checkboxComponent{
}
app.components.ts
import {Component} from '@angular/core';
import {checkboxComponent} from './extCheckbox.Component';
@Component({
selector: 'my-app',
template: '<h1>checkboxComponent</h1><ext-mycheckbox></ext-mycheckbox>',
directives:[checkboxComponent]
})
export class AppComponent {
}
选择器 my-app 元素已在我的 index.html 中给出,但复选框未在我的页面中呈现。
【问题讨论】:
标签: javascript angularjs html angular typescript