phan ngoc phan ngoc - 5 months ago 16
AngularJS Question

How to change function in ng-submit angularjs

The situation occur when i want to convert form create into update form, so i need create() in ng-submit change to update(). How do i can make it ? .Concrete example:

<form name="articleForm" class="form-horizontal" ng-submit="create()" novalidate>


to

<form name="articleForm" class="form-horizontal" ng-submit="update()" novalidate>


Thank you !

Answer

What about :

<form name="articleForm" class="form-horizontal" ng-submit="submit()" novalidate>

and then have something like this in your controller :

$scope.isNewUser = typeof $scope.user.id === 'undefined';

function create() {...}
function update() {...}

$scope.submit = function () {
    $scope.isNewUser ? create() : update();
}