I am newbie in angularJs. I am trying to re-run init() function inside link when 'localid' has been changed, but it said
var elm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
is null even it works at the first time on page load.
TypeError: Cannot read property 'getElementsByTagName' of null
I googled it a lot for more than 5 hours but i couldn't find the reason. What do I missing ?
(function() {
'use strict';
angular
.module('neuroSeoulFinance')
.directive('seoulmap', seoulMap);
/** @ngInject */
function seoulMap($log) {
var directive = {
restrict: 'E',
template: '<object id="seoulmap" class="localMap" data="../assets/images/seoul.svg" type="image/svg+xml" width="80%"></object>',
replace: true,
scope: {
localid : "="
},
link: function(scope, element, attrs) {
var init = function() {
var elm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
$log.log(elm);
for(var i=0; i<elm.length; i++) {
if(elm[i].id == scope.localid.id) {
elm[i].style.fill = attrs.highlight;
}
else {
elm[i].style.fill = "#e1e3e4";
}
}
};
scope.$watch('localid', function(newValue, oldValue) {
if (newValue){
init(); // <-- it doesn't work.
// var ealm = angular.element(element[0].getSVGDocument().getElementsByTagName("path"));
// ^ it also return null..
$log.log("I see a data change!");
}
}, true);
if(element[0].getSVGDocument()) {
$log.log("init");
init();
} else {
$log.log("load");
element.on("load",init);
}
}
};
return directive;
}})();