AngularJS:调用形式外NG-提交事件 [英] AngularJS: Call the ng-submit event outside the form

查看:156
本文介绍了AngularJS:调用形式外NG-提交事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS鱼,我有这样的情况。

I'm a fish in AngularJS and I have this scenario.

<form>
    <input type="text">
</form>
<button type="submit">submit</button>

在正常方式AngularJS提供了NG-提交指令,因为在形式的属性来工作,但我需要的外部调用它。

In normal ways AngularJS provides the ng-submit directive to work as an attribute in the form but I need to call it outside.

所以,有人经历了同样的问题?如果是的话,你做了什么?

So, someone has experienced the same problem? If yes, what you did?

推荐答案

请,环绕你的code与NG-控制器和按钮上使用NG-点击出的&LT范围;形式的方式&gt;

Please, surround your code with a ng-controller, and use ng-click on buttons out of scope of <form>.

我作出的jsfiddle你一个​​样本......试试吧:

I make a sample on jsfiddle for you... try it:

http://jsfiddle.net/xKkvj/2/

<div ng-app>
    <div ng-controller="Ctrl">
        <form ng-submit="submit()">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>
        <button ng-click="submit()">Submit 2</button>
    </div>
</div>

与JS:

function Ctrl($scope) {
    $scope.list = [];
    $scope.text = 'hello';
    $scope.submit = function () {
        if ($scope.text) {
            $scope.list.push($scope.text);
            $scope.text = '';
        }
    };
}

这篇关于AngularJS:调用形式外NG-提交事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆