我有一個控制器類的組件,它使用綁定對象作爲輸入參數。在構造函數運行後,我可以訪問控制器中的參數。但我無法將其作爲構造函數參數傳遞。解決與控制器類的綁定
製作一個plunker我找到了一個使用$ onInit方法的posible解決方案。 (ui-router doc)。
但這是正確的方法嗎?希望這有助於某人,請告訴我它是否可以改進。
這是控制器:
class Controller {
constructor(){
'ngInject';
this.$onInit = function(){ // Comment this
this.problemScopeHere = angular.copy(this.param);
} // Comment this
console.log(this.param);
}
get(){
this.problemScopeHere = this.param;
}
}
這裏組件
let Component = {
bindings : {
param : "="
},
controller : Controller,
controllerAs: '$ctrl',
templateUrl: 'component.html'
};
let app = angular.module('app', ['ui.router']);
app.component('comp', Component);