angularjs – 通过ng-submit找到触发按钮

前端之家收集整理的这篇文章主要介绍了angularjs – 通过ng-submit找到触发按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何通过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>
您可以在提交之前使用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对于ng-submit不起作用,如果你对使用$event.target感兴趣(无论如何不应该从控制器完成)

原文链接:https://www.f2er.com/angularjs/142187.html

猜你在找的Angularjs相关文章