I'm trying to invoke a parent method from within a component using ng-click in Angular 1.6.7. Should I instead be using $emit somehow? I am currently attempting to do it via on-click='$ctrl.$parent.myMethod() and it is not invoking the method. Code below, live link here: http://jsbin.com/cenubalola/edit?html,js,output
JS
var app = angular.module('app', []);
app.controller('mainController', ['$scope', function ($scope) {
$scope.cars = [
{ make: 'civic' },
{ make: 'rav4' }
];
$scope.openEditCar = function(c) {
$scope.editCar = c;
};
$scope.saveCar = function() {
// save car to db
alert('saving...');
$scope.editCar = null; // hide the component
};
}]);
app.component("editCar", {
template: 'Editing <input ng-model="$ctrl.editCar.make" /> - <a href="#" ng-click="$ctrl.$parent.saveCar()" onclick="return false;">Save</a>',
bindings: { editCar: '=' }
});
HTML
<div ng-repeat='c in cars'>
{{c.make}} - <a href='#' ng-click='openEditCar(c)' onclick='return false;'>Edit</a>
</div>
<edit-car edit-car='editCar' ng-show='editCar'></edit-car>
<div>
editCar is {{editCar | json}}
</div>