2

I tried to get the HTML page value to angularJS function , The below steps are which i tried.

HTML page :

<label class="item-input item-stacked-label">
    <span class="input-label cont_det_label">First Name</span>
    <p class="contact_display" id="txtFirstName" ng-model="testName">Satya</p>
  </label>

angularJS Page :

.controller('SocialNetworkCtrl', ['$scope','$http','$state','ContactsService','$ionicNavBarDelegate','$ionicLoading','$ionicPopup',function($scope, $http, $state, ContactsService, $ionicNavBarDelegate, $ionicLoading,$ionicPopup) {

                                  $scope.showUserProfile = function() {
                                  $state.go("linkedin");

                           var firstname = (document.getElementById("txtFirstName").value);


                                  }
                                  }])

So I need var firstname = Satya ?? Is it correct way please guide me to access this value .

2
  • 1
    ng-model in <p>? Commented Jan 22, 2015 at 7:44
  • Study some angular tutorials. The one in documentation site is well worth the time spent Commented Jan 22, 2015 at 8:53

2 Answers 2

1
var firstName = $scope.testName

<input ng-model="testName" />

testName is the ng-model name that you have give. It will be automatically binded to your controller. No need the get the value using document.getElementById

Wrong usage , why ng-model in <p> tag??

Update

Change your fiddle with the following code, it will work. Also make sure framework is selected properly (as in the image) FrameWork and Extension

<div ng-app ng-controller="testController">
    <input ng-model="testDataName" ng-change="check()" /> {{testDataName}}
    After ng-change : {{checkName}}
</div>

 function testController($scope) {
    $scope.testDataName="Dummy Name";
    $scope.check = function () {
        $scope.checkName=$scope.testDataName;
        console.log($scope.checkName);
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

HI Jan thanks for your valuable reply but my scenario is different , Because actually what i need is for example my contactDetail Screen I've a paragraph value like Suthan M , So next Screen I need this paragraph value in the variable (eg: var testName = the paragraph value) , Finally i need get the <p> tag value already in html page , that value i need in the JS page is it clear i tried so many ways but not able to get the value.
1

its a text node, you will require .innerHTML or '.innerText', .value is for form inputs

 var firstname = (document.getElementById("txtFirstName").innerHTML);

and don't use ng-model on a p element, change it to like this

<p class="contact_display" id="txtFirstName">{{testName}}</p>

just use $scope.testName to get the value, no need for firstname = (document.getElementById("txtFirstName").innerHTML); querying DOM for value is jQuery style, use angular the $scope for 2 way bindings

Read more at official doc

Update here is updated function on loginCtrl

.controller('loginCtrl', ['$scope', function ($scope) {
     $scope.testNameData = 'Satya';
     $scope.doLogin = function() {
        alert($scope.testNameData);
     };
}])

If you really want to go jQuery way here is what you can do, its not recommended, you should use angular directive to do DOM manipulation

$scope.showUserPro = function() {

    $ionicLoading.show();

    // Here i need the value of <p tag>
    var name = document.getElementById("txtFirstName"),
        firstNameFromHtmlPtag = name.innerText;

    console.log(firstNameFromHtmlPtag, 'Doing API Call 1');

}

11 Comments

Thanks for your quick reply Saqueib , But i tried both .innerHTML and .value but no response received in the var firstname ??
As per your point i tried but getting "undefined" in the alert for firstname ??
Sorry i can not create a fiddle r8 now , But i need to know in the HTML page <p class="contact_display" id="txtFirstName">{{testName}}</p> where can i insert the value Satya , I tried after testName but getting Undefined ????
HI Saqueib , as per your note this way for set the value right , But i need get the <p> value in the JS page.
you can use {{testName}} in <p> tag, it will be binded, if you change the scope $scope.testNameData it will always update the <p> tag, can you tell whats you want to build
|

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.