Vishal Mahuli Vishal Mahuli - 5 months ago 14
AngularJS Question

How to pass value from a textbox to a function in AngularJs?

My HTML:

<input type ="text" ng-model="contactid" >


I want to pass the contactId value entered in this box to a function
abc()
which is called
onclick
of submit button after the box.

How do I do this?

Answer

You can do it like so:

<form ng-controller="formCtrl">     
    <input type="text" name="name" ng-model="inputValue" />
    <button ng-click="abc(inputValue)"></button>
</form>

Or using ng-submit directive:

<form ng-controller="formCtrl" ng-submit="abc(inputValue)">     
    <input type="text" name="name" ng-model="inputValue" />
    <button type="submit"></button>
</form>

And in your formCtrl controller:

.controller('formCtrl', function () {
    $scope.inputValue = null;
    $scope.abc = function (value) {
        console.log(value);
    };
});
Comments