通过ng-submit找到触发按钮

时间:2012-10-21 08:18:53

标签: angularjs

如何通过 ng-submit 形式 找到触发按钮 并点击此按钮

<form ng-submit="submit()" ng-controller="Ctrl">
  <input type="submit" att1="A" att2="B" value="Edit" />
  <input type="submit" att1="C" att2="D" value="Delete" />
</form>

<script>
  function Ctrl($scope) {
    $scope.submit = function() {
      alert(this.att1)
      alert(this.att2)
    }
  }
</script>

1 个答案:

答案 0 :(得分:13)

您可以在提交之前使用ng-click更改范围内的值

<form ng-submit="submit()" ng-controller="Ctrl">
  <input type="submit" ng-click="setAtts('A', 'B')" value="Edit" />
  <input type="submit" ng-click="setAtts('C', 'D')" value="Delete" />
</form>

<script>
  function Ctrl($scope) {
    $scope.submit = function() {
      alert($scope.att1);
      alert($scope.att2);
    };

    $scope.setAtts = function(a1, a2) {
      $scope.att1 = a1;
      $scope.att2 = a2;
    };
  }
</script>

编辑:如果您对使用$event感兴趣,那么旁注$event.target将不适用于ng-submit(无论如何不应该从控制器完成)