0

I need angularjs controller to read hidden input field on a form. I tried to define a scope variable in the controller 'postData.TransactionId' and look at this in the controller. But the value is coming as blank. Is it because DOM is not loaded yet for angularjs controller to look at the input field? Are there any alternatives? Thank you!

<form name="form1" id="form1" ng-controller="Ctrl">
    <input type="hidden" id="TransactionId" name="TransactionId" value="@Request.Form["TransactionId"]" ng-model="postData.TransactionId">
</form>

I am trying to inject a service in the script tag of the view but I get an error saying "angular is undefined". Is it possible to read the value in Ctrl this way?

<div ng-controller="Ctrl">
</div>

<script src="~/Scripts/angular-idle.min.js"></script>
<script>

    var app = angular.module('pay', []);
    app.service("PostService", function () {
        this.TransactionID = "Test";
    });
</script>
4
  • You can use page life hooks in angular . start you operation in $onInit . blog.thoughtram.io/angularjs/2016/03/29/… I hope it helps Commented Feb 22, 2019 at 20:50
  • This is not possible with a hidden input. ngModel does not do binding with hidden inputs Commented Feb 22, 2019 at 21:22
  • Maybe $element is what you are looking for? Commented Feb 23, 2019 at 14:49
  • 1
    Possible duplicate of Angularjs: Get element in controller Commented Feb 23, 2019 at 14:54

1 Answer 1

0

You could try using ng-init to fill the model

<input type="hidden" id="TransactionId" name="TransactionId" value="@Request.Form["TransactionId"]" ng-model="postData.TransactionId" ng-init="[email protected]["TransactionId"]">

or you could use input type="text" with display style none link

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

2 Comments

I tried to define input elements as suggested but it did not work for me. I think my problem is that controller is invoked first before DOM is loaded. So the controller is still showing blank value in the model postData.TransactionId. Thank you.
I am trying to pass the values to angular service and read them in the controller. But I get an error "angular is undefined".

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.