Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  27] [ 4]  / answers: 1 / hits: 78782  / 11 Years ago, mon, september 2, 2013, 12:00:00

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



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


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?


More From » html

 Answers
18

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



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>


with js:



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

[#75952] Saturday, August 31, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;