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/

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

4 Comments

value field is not needed when ng-model is used
I added an example for you
There is a convension that model name should have a dot inside it otherwise it'll lost in the $scope's realm
Hi , Preston. I can't use ng-init because this value will be filled from a code-behind Asp.net page...
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 Comments

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.
unless you use the controller to set the ng-model to what was given by asp.net
@GCoe see my answer about initializing the model from the input tag
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 other
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.
|
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 Comments

Accessing to the DOM from a controller is a bad practice. DOM should be accessed only inside directives.
@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.

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.