I tried to extend the "Heros" application from the AngularJS documentation
I wanted to add the functionality of creating/adding a hero.
In the HeroList.js from this plunk I am able to add a new hero after clicking the button Add from the HeroList.html.
However, if I'm updating the input fields (let's say name) they get edited in the list as well.
For example, if I added a new hero (Captain America), click Add, and then type Robby, Captain America will be concatenated with Robbie.
(function(angular) {
'use strict';
function HeroListController($scope, $element, $attrs) {
var ctrl = this;
// This would be loaded by $http etc.
ctrl.list = [
{
name: 'Superman',
location: ''
},
{
name: 'Batman',
location: 'Wayne Manor'
}
];
ctrl.create = function(hero) {
ctrl.list.push(hero);
};
ctrl.updateHero = function(hero, prop, value) {
hero[prop] = value;
};
ctrl.deleteHero = function(hero) {
var idx = ctrl.list.indexOf(hero);
if (idx >= 0) {
ctrl.list.splice(idx, 1);
}
};
}
angular.module('heroApp').component('heroList', {
templateUrl: 'heroList.html',
controller: HeroListController,
bindings: {
onCreate: '&'
}
});
})(window.angular);