(function() { 'use strict'; angular .module('heroApp', []) .component('heroList', { template: ` Heros
`, controller: function() { var $ctrl = this; $ctrl.list = [ { name: '张三', location: 'test1' }, { name: '李四', location: 'test2' } ]; $ctrl.removeHero = removeHero; $ctrl.updateHero = updateHero; function removeHero(hero) { var idx = $ctrl.list.indexOf(hero); if (idx >=0 ) { $ctrl.list.splice(idx, 1); } } function updateHero(hero, prop, value) { hero[prop] = value; } } }) .component('heroDetail', { template: `
Name:{{$ctrl.hero.name}} Location:
`, bindings: { hero: '<', onRemove: '&', onUpdate: '&' }, controller: function() { var $ctrl = this; $ctrl.remove = remove; $ctrl.update = update; function remove() { $ctrl.onRemove({ hero: $ctrl.hero }); } function update(prop, value) { console.log(prop); $ctrl.onUpdate({hero: $ctrl.hero, prop: prop, value: value}); }; } }) .component('editableField', { template: ` {{$ctrl.fieldValue}} `, bindings: { fieldValue: '<', fieldType: '@?', onUpdate: '&' }, controller: function() { var $ctrl = this; $ctrl.editMode = false; $ctrl.$onInit = init; $ctrl.reset = reset; $ctrl.handleModeChange = handleModeChange; function handleModeChange() { if ($ctrl.editMode) { $ctrl.onUpdate({ value: $ctrl.fieldValue }); $ctrl.fieldValueCopy = $ctrl.fieldValue; } $ctrl.editMode = !$ctrl.editMode; } function reset() { $ctrl.fieldValue = $ctrl.fieldValueCopy; } function init() { $ctrl.fieldValueCopy = $ctrl.fieldValue; $ctrl.fieldType = $ctrl.fieldType || 'text'; } } }) })();