I would like to be able to create a angularjs directive that in the end creates a HighCharts graph from the elements attributes. I have a directive working but any dynamically set attributes are returning undefined. In the end i want all the attributes on the element to be sent to a $http request as params.
I have tried using the $observe method and it does not seem to work, also i would like the attributes to be anything and not predefined.
See this fiddle: http://jsfiddle.net/F52SF/ and code below of directive:
myApp.directive('example', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
console.log('Log1:', attrs.exampleId);
attrs.$observe('exampleAttr', function () {
console.log('Log2:', attrs.exampleId);
});
var data = {};
for(var i in attrs){
var t = Object.prototype.toString.call( attrs[i] );
if(t !== '[object Object]' && t != '[object Function]') data[i] = attrs[i];
}
console.log(data);
}
}
});
In your example code there are couple of mistakes/improvements:
Its better to read value passed to callback of $observe function:
attrs.$observe('exampleAttr', function(newValue) {
/* use newValue here instead of attrs.exampleAttr */
});
Here is working fiddle: http://jsfiddle.net/F52SF/1/