Я довольно новыми для node.js и я планирую перенести наши угловые 1 директивы компонентов для того, чтобы облегчить переход к Угловая 2.Угловое 1 компонент против определения директивы
Итак, мой вопрос является то, что у меня есть некоторый код, работающий с несколькими определениями, как это:
'use strict';
var angular = require('angular');
angular.module('dashboard')
.directive('yepNope', require('./yep-nope.directive'))
//.component('comp', require('./myFirstComponent.component'));
.component('comp', new (require('./myFirstComponent.component')));
и да-nope.directive и MyFirstComponent.component определяются таким же образом:
'use strict';
function MyFirstComponent() {
function componentController($element){
var vm = this;
init();
function init(){
vm.api = {
bar : function(){
console.log('bar called');
},
foo : function(){
console.log('foo called');
}
};
}
this.$onInit = function(){
console.log("$onInit");
};
this.$postLink = function(){
console.log("$postLink");
};
this.$onChanges = function(changesObj){
console.log("$onChanges");
};
}
return {
bindings: { },
controller: componentController,
//controllerAs: '$ctrl',
template:'<div><h1>My Component header</h1></div>'
}
}
module.exports = MyFirstComponent;
в d
'use strict';
function YepNopeDirective() {
return {
restrict: 'E',
link: function (scope, element, attrs) {
scope.$watch(attrs.check, function (val) {
var words = val ? 'Yep' : 'Nope';
element.text(words);
});
}
}
}
module.exports = YepNopeDirective;
Есть ли объяснение, почему определение компонент мне нужно сделать новый (требуется ... в то время как с директивой это не нужно?
.directive('yepNope', require('./yep-nope.directive'))
//.component('comp', require('./myFirstComponent.component'));
.component('comp', new (require('./myFirstComponent.component')));
Спасибо,
Дэвид.
Забудьте об этом простом примере. Директивы нашего проекта не совместимы с Angular 2 (мы используем компиляцию и замену среди других). В этой ситуации нам нужно преобразовать директивы в компоненты. В любом случае, мой вопрос в том, почему мне нужно сделать новое (требуется при использовании компонента, когда с директивой, которой я не обязан. – David