Home > OS >  How to throw html input values into ng-click function with angularjs
How to throw html input values into ng-click function with angularjs

Time:12-12

Hello guys i am learning html and angularjs at the same time.I don't know how i can throw simple input into my functions.

Random

<input id="input1" type="text">

<button ng-click="someAction()">click me</button>

Basically i want to put the text value from the input into someAction method. Sorry if my code or question is confusing. It confuses me too.

How can i do it?

CodePudding user response:

You've to use ngModel and $scope to save the function

angular.module('starter', [])
  .controller('MainCtrl', function($scope) {
    $scope.someAction = function(name) {
      $scope.result = 'Hi '   name;
    }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.2/angular.min.js"></script>
<div ng-app="starter" ng-controller="MainCtrl">
  <!-- ngModel will store the input data, it's as if you were creating a variable -->
  <input type="text" ng-model="name" placeholder="Your name"/>
  <button ng-click="someAction(name)">Some action</button>
  <p>{{result}}</p>
</div>

  • Related