0

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?

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.