15

I'm trying to get the value from an HTML element from Angular controller.

 <div ng-app="myApp" ng-controller="myControler"> <br /> <input id="Text1" type="text" runat="server" value="aValue" / 

My controller :

var myApp = angular.module('myApp', []); myApp.controller("myControler", function ($scope, $document) { var name = angular.element($('#Text1')).val(); console.log(name); }); 

But name returns "undefined"...

Please, what I'm doing wrong ?

    3 Answers 3

    17

    angular.element is an alias for jquery $.

    You could access that element like this: angular.element('#Text1').val();

    ng-model is the angular way to do this though. You can set the value from ASP using ng-init

    <input id="Text1" type="text" runat="server" ng-model="inputVal" ng-init="inputVal='aVal'"> 

    And this can be accessed from the controller using the scope console.log($scope.inputVal);

    JSfiddle example: http://jsfiddle.net/n1oppeu2/

    4
    • value field is not needed when ng-model is usedCommentedJul 12, 2015 at 2:06
    • I added an example for youCommentedJul 12, 2015 at 2:19
    • There is a convension that model name should have a dot inside it otherwise it'll lost in the $scope's realmCommentedJul 12, 2015 at 2:24
    • Hi , Preston. I can't use ng-init because this value will be filled from a code-behind Asp.net page...
      – GCoe
      CommentedJul 12, 2015 at 2:25
    7

    Why you need angular element to access form element ??

    You can get and set value by binding model to it

    like this

    <input id="Text1" type="text" runat="server" ng-model="input.field1" /> 

    controller

    $scope.input={ field1:'' } 
    6
    • This element "Text1" will be filled from a code-behind Asp.net form.Then I'll use it's value inside the controller. If I bind it to a scope variable, the controller reset it's value.
      – GCoe
      CommentedJul 12, 2015 at 2:12
    • unless you use the controller to set the ng-model to what was given by asp.net
      – J-Dizzle
      CommentedJul 12, 2015 at 2:12
    • @GCoe see my answer about initializing the model from the input tagCommentedJul 12, 2015 at 2:15
    • I just wanna to know , If you will access everything from .cs file then why you need angular for ? If you wanna angular badly, then try to use server side code only data save and retrive and let angularjs handle otherCommentedJul 12, 2015 at 2:22
    • Hi Anik, I'm trying to pass a collection of points ( company name, lat, long ) from .cs to a controller that will plot these points at a map with ngmap.
      – GCoe
      CommentedJul 12, 2015 at 2:31
    2

    Using angular element selector #

    <input id="Text1" type="text" runat="server" value="aValue" /> console.log(angular.element('#Text1').val()); 

    or

    console.log(angular.element('#Text1')[0].value); 
    2
    • 1
      Accessing to the DOM from a controller is a bad practice. DOM should be accessed only inside directives.
      – mpolci
      CommentedJul 23, 2016 at 9:03
    • @mpolci I've been trying to figure out how to get input in AngularJS for several days now; please elaborate with a thorough example in which I can take the user input and then compare it to data inside of the controller.
      – MadHatter
      CommentedApr 3, 2019 at 19:00

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.