Avi
Avi

Reputation: 1964

Angularjs - get value from external DOM element

I am writing an angular application as module inside an existing application. There is a hidden field in the outer application that I need.

My question is what is the best way to fetch this value and use it in my angular application?

Thanks.

Upvotes: 0

Views: 119

Answers (2)

Vineet
Vineet

Reputation: 4635

Add your attribute in window object from anywhere in your code, use

<script>
window.my_value = 'value';
</script>

And in your controller, please inject $window service and then use

app.controller("MyController", function ($scope, $window) {
console.log($window.my_value);
})

OR

You can directly use this hidden field's value, like

var obj = angular.element(document.querySelector("#hiddenFieldId"));
console.log(obj.val());

If you have an ID/Class for that input..

Upvotes: 1

Tomas
Tomas

Reputation: 2726

You can always get the value with pure JS:

var input = document.getElementById('id');

Upvotes: 0

Related Questions