javascript - 如何在按下按钮时更新 angularjs 模型(使用 ng-model-options 参数)?

标签 javascript angularjs

我有一些输入(文本、复选框、选择)。

我只想在按下按钮后更新 Controller 模型。

<input type="email" ng-model="vm.email" ng-model-options="{updateOn:'not blur, maybe custom event'} }"/>
<button ng-click="vm.apply()">Apply</button>

vm.apply = function() {
    //How to fire event to trigger model update?
}

最佳答案

好的,我想我找到了解决方案。我们可以使用提交事件。 因此只有当用户按下提交按钮时,模型才会更新值。

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example79-production</title>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
</head>
<body ng-app="submitExample">
  <script>
  angular.module('submitExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.list = [];
      $scope.text = 'hello';
      $scope.submit = function() {
        if ($scope.text) {
          $scope.list.push(this.text);
        }
      };
    }]);
</script>
<form ng-submit="submit()" ng-controller="ExampleController">
  Enter text and hit enter:
  <input type="text" ng-model="text" ng-model-options="{ updateOn: 'submit' }" name="text" />
  <input type="submit" id="submit" value="Submit" />
  <pre>list={{list}}</pre>
  <pre>text={{text}}</pre>
</form>
</body>
</html>

关于javascript - 如何在按下按钮时更新 angularjs 模型(使用 ng-model-options 参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31694315/

相关文章:

javascript - 如何通过 clickHandler 进行导航?

javascript - 显示按钮 B 时仅显示按钮 A

Javascript/jQuery Exec 结果为 Null

javascript - scope.$watch 不会因变量更改而触发

javascript - 使每个指令的指令范围都是唯一的?

css - 带有空名称/标签的 Uib 下拉列表未打开

javascript - 删除字母符号,但不删除

javascript - 子组件reactjs的动态props

javascript - Angular-route1.6.1 无法正常工作

javascript - angularjs做一个简单的倒计时