我想通過我的組件傳遞一個值到我的控制器,但我仍然得到這個未定義的消息。我錯過了什麼嗎?如何將值傳遞給使用組件的控制器?
myhtml.html
<my-component item="value1"></my-component>
<my-component item="value2"></my-component>
<my-component item="value3"></my-component>
mycomponent.js
angular
.module('mycomponent.component', [
'mycomponent.controller'
])
.component('myComponent', {
templateUrl: 'components/mycomp/mycomp.component.html',
controller: 'ComponentController',
controllerAs: 'componentVM',
replace: true,
bindings: {
item: '='
}
});
mycontroller.js
angular
.module('mycomponent.controller', [])
.controller('ComponentController', ComponentController);
ComponentController.$inject = [];
function ComponentController() {
var vm = this;
console.log('item value> ' + vm.item); // This is got undefined :(
}
您需要在'this。$ onInit = function(){/ * bound properties available here * /}內放置'console.log()'(或者任何使用vm.item的東西)' – mhodges