©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
让Angular表达式可以绑定到onsubmit事件上。
此外,它可以防止默认动作(这对表单意味着向服务器发送请求和重新加载当前页),但只在表单没包含action
, data-action
或x-action
属性时。
<form
ng-submit="">
...
</form>
参数 | 类型 | 详述 |
---|---|---|
ngSubmit | expression | 用于计算的表达式。 (事件对象存放于 |
<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);
$scope.text = '';
}
};
}]);
</script>
<form ng-submit="submit()" ng-controller="ExampleController">
Enter text and hit enter:
<input Type="text" ng-model="text" name="text" />
<input Type="submit" id="submit" value="Submit" />
<pre>list={{list}}</pre>
</form>
it('should check ng-submit', Function() {
expect(element(by.binding('list')).getText()).toBe('list=[]');
element(by.css('#submit')).click();
expect(element(by.binding('list')).getText()).toContain('hello');
expect(element(by.model('text')).getAttribute('value')).toBe('');});
it('should ignore empty strings', Function() {
expect(element(by.binding('list')).getText()).toBe('list=[]');
element(by.css('#submit')).click();
element(by.css('#submit')).click();
expect(element(by.binding('list')).getText()).toContain('hello');
});