![](/img/trans.png)
[英]Angular 1.5 component - template not rendered after converting from directive
[英]Angular Directive to component angular 1.5
我有這個指令,我想做一個組件
angular.module('app')
.directive('year', function () {
var controller = ['$scope', function ($scope) {
$scope.setYear = function (val) {
$scope.selectedyear = val;
}
}];
return {
restrict: 'E',
controller: controller,
templateUrl: "views/year.html"
};
});
這是我到目前為止所得到的:
angular.module('app')
.component('year', {
restrict: 'E',
controller: controller,
templateUrl: "views/year.html"
});
我不確定如何將var controller
移動到.component
您應該做的幾件事將指令轉換為組件
因此您的組件應如下所示...
angular.module('app')
.component('year', {
controller: ComponentController,
templateUrl: "views/year.html"
});
function ComponentController(){
var $ctrl = this;
$ctrl.setYear = function (val) {
$ctrl.selectedyear = val;
}
}
您的組件應如下所示:
function YearController() {
var $ctrl = this;
$ctrl.setYear = function (val) {
$ctrl.selectedyear = val;
}
}
angular.module('app').component('year', {
templateUrl: 'views/year.html',
controller: YearController
});
有關更多詳細信息,請閱讀以下堆棧溢出問題以獲取更多詳細信息: Angular 1.5組件與舊指令-鏈接函數在哪里? 以及官方文檔: https : //docs.angularjs.org/guide/component
另外,請注意,默認情況下,組件僅限於元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.