Jdenticon icons not rendering when using ng-repeat

586 views Asked by At

I'v been using Jdenticon JavaScript library (https://jdenticon.com/) for my user icons. It should take hash and render it as either SVG or Canvas using something like this:

<svg width="200" height="200" data-jdenticon-hash="ff8adece0631821959f443c9d956fc39">
    Fallback text for browsers not supporting inline svg
</svg>

So the problem is that I am trying to render multiple user icons on single page using angular ng-repeat and bind hash inside data-jdenticon-hash. It appears that all data is right where it should be, but Jdenticon is complaining that it doesnt see the binded data. If I add static hash, like "ff8adece0631821959f443c9d956fc39" inside data-jdenticon-hash, it renders all icons the same, but correctly.

Here is my current code:

<div ng-repeat="i in friends" last-element-directive>
<div id="requests" class="col col-md-12 col-sm-12 col-xs-12 tab-pane fade in active" ng-show="user_friends">
      <div id="icon" class="col-md-12">
        <div class="col-md-1 col-sm-1 col-xs-4">
          <svg width="40" height="40" data-jdenticon-hash="{{i.avatar}}"></svg>
        </div>
        <div class="col-md-3 col-sm-2 col-xs-3">
          <h3><a href="/user/?id={{i.username}}" target="_blank">{{i.username}}</a></h3>
        </div>
      </div>
    </div>
</div>

Any help would be appreciated!

1

There are 1 answers

3
Ursin Brunner On BEST ANSWER

I'm not an expert with jdention but just stumbled over this while implementing it for my project. Maybe it helps you:

The problem is that jdenticon.update is never called for canvases created dynamically as by Angular. A solution could be to create a directive that is responsible for calling jdenticon.update when the canvas element is constructed. See this fiddle for an example: https://jsfiddle.net/w5h6msvd/

source is this github issue: https://github.com/dmester/jdenticon/issues/10

Edit: this is how I use it now in my project

import identiconImpl from 'jdenticon';

export default () => ({
    restrict: 'A',
    link: (scope, elem) => {
        identiconImpl.update(elem[0], scope.hashValue);
    },
    scope: {
        hashValue: '<'
    }
});

And this is the template:

<svg identicon hash-value="ctrl.hashAndSaltOperatorName()"></svg>

Oh and here the index.js to have all the necessary parts:

export default angular
    .module('jdenticonHash', [])
    .directive('identicon', identiconDirective);