0

I have to get the value of an input text with AngularJS but without using Controller. How i can get this value? I saw this posts but uses .controller link post

4
  • You can use angular.element: docs.angularjs.org/api/ng/function/angular.element Commented Jan 13, 2016 at 11:35
  • Just curious: why don't you use a controller? Commented Jan 13, 2016 at 11:36
  • we can not use a controller. project requirements.... Commented Jan 13, 2016 at 11:46
  • why use angularJS then? Commented Jan 13, 2016 at 11:53

3 Answers 3

1

You can use this code, with angular.element:

angular.element(document.querySelector('#your_input_tag_id')).val();

or, with simple jQuery:

$('#your_input_tag_id').val();
Sign up to request clarification or add additional context in comments.

Comments

1

make your input a model and the value will always be available as the model

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

and then your value will be available within the scope of your module as myModelName

console.log('value = ' + $scope.myModelName);

, if you are trying to get the value from somewhere other than the scope of the module or app then you are probably doing it wrong and not the angular way, it really is best to use a controller or link function even if it's the main app controller or link function, and you would be well placed to push back on any requirement not to use a controller (which sounds like a bad or misunderstood requirement).

Comments

0

Rather than querying the DOM for elements (which isn't very angular see How do I "think in AngularJS" if I have a jQuery background?) you should perform your DOM manipulation within your directive. The element is available to you in your link function.

So in your myDirective

return { link: function (scope, element, attr) { element.html('Hello world'); } }

If you must perform the query outside of the directive then it would be possible to use querySelectorAll in modern browers

angular.element(document.querySelectorAll("[my-directive]"));
$('#your_input_tag_id').val();

however you would need to use jquery to support IE8 and backwards

angular.element($("[my-directive]"));

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.