Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularjs: How to read scope rendered value with Jquery
    primarykey
    data
    text
    <p>Im working on an angularjs project. And really want to know how can I access values that rendered by scope by jquery ( or other javascript codes). Here is a part of my template:</p> <pre><code>&lt;div class="bars" data-label="Population" data-val="{{town.population}}" data-color="#cccccc"&gt; </code></pre> <p>So, Im going to use the value of the attribute of <code>data-val</code> which is rendered by scope. I'm doing it like this:</p> <pre><code>TownApp.directive('bars', function () { return { restrict: 'C', link: function (scope, elem, attrs) { // I tried : this will get value: {{town.population}} as a string itself. $(".bar").jqbar({value: $(this).data('val'); // also tried: this will get undefined elem.jqbar({ value: attrs.val}); // and so on...... } } } </code></pre> <p>})</p> <p>As I described in the comments, seems while the directive is running, the scope value is still not rendered to page, so that the further codes can not read the value properly.</p> <p>So, my questions is how can I do this? Thanks a lot !</p> <p>UPDATE: </p> <p>Adding controller and factory codes:</p> <pre><code>var TownApp=angular.module('TownApp',['ngRoute','ngResource']); TownApp.factory('Town', function($http) { return $http.get('/database.json').then(function(response){ return response.data; }) }); var TownCtrl = TownApp.controller('TownCtrl',function($scope, $routeParams, Town, $location){ Town.then(function(response){ $scope.towns = response; for (var i = 0; i &lt; response.length; i++) { if(response[i].Code === $routeParams.townId){ var currentTown = response[i]; } $scope.town = currentTown; } //....... }); </code></pre> <p>})</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload