Jump to content

Assigning Angular value to JavaScript variable in HTML


eeee

Recommended Posts

I'm working with a web reporting framework that makes data available through Angular. I can reference a field using Angular in HTML. I would like to do some processing based on a field value and this requires JavaScript. I want to assign the Angular value to a variable in JavaScript but I have no idea how to do this (I'm not a web developer and I know very little about JavaScript and nothing about Angular).

So in my HTML code if I write 

<p>{{FolderDetails[0].RowId}}</p>

the screen will display a value e.g. 12345

I tried the following in my HTML code but nothing displays
 

<script>   
  document.write('Hello World!');   
  
  //Get the value from Angular and assign to a JavaScript variable  
  var RowId = {{FolderDetails[0].RowId}};  
  
  document.write('RowId = ' + RowId);

</script>

Thanks in advance.

Link to comment
Share on other sites

The method I use for this (because my JS is in a separate file) is to place the value in a hidden element, and then retrieve the value from there. If this value updates as you use it, just remember to get the value again each time you use it.

I'm not too familiar with angular bindings to know what happens if you change it with JavaScript.

<span id="rowID" style="display:none">{{FolderDetails[0].RowId}}</span>

<script>
  var rowID = document.getElementById("rowID").textContent;
  //Now do stuff with your newly gained Angular Value
</script>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...