简体   繁体   中英

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 !

Just use a ternary with one variable defined in the $scope:

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

in your controler define:

$scope.isCreation = true;

and change it to false when you want update() to be triggered

Either you could use ng-if determine which one is needed, or use the same function and check inside the function if you are in create or update mode. You could also use directives and pass the function to your directive, depending how you want to use this form, because that is not perfectly clear for me.

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();
}

define $ scope.isCreate = true; //对于创建为true,对于在Angular Controller中和窗体处的更新为false

<form novalidate ng-submit="isCreate?create():update()">

Little Later But Useful

      <form class="form-horizontal"  name="BannerForm"  
       ng-submit="call(submitOption)" novalidate>


 //**For MultiSubmit
   $scope.call = function(expr) {
    return $scope.$eval(expr);
  };

//For InsertNew
    $scope.InsertData= function(){
    $scope.submitOption="add()";
    }


//For UpDate
    $scope.UpdateData= function(){
    $scope.submitOption="update()";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM