简体   繁体   English

如何在ng-submit angularjs中更改功能

[英]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(). 当我想将表单创建转换为更新表单时会发生这种情况,因此我需要在ng-submit更改中将create()更改为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: 只需使用在$ 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 并在要触发update()时将其更改为false

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. 您可以使用ng-if确定需要哪个,或者使用相同的函数并在函数内部检查是否处于创建或更新模式。 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()";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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