I have an example of the problem I am facing here: http://jsfiddle.net/Siyfion/JxLhX/
Essentially, I want to use jQuerySVG to attach some functionality to the SVG document (onClick, etc.) whenever the user clicks on a document from the list.
I know that DOM manipulation shouldn't be done in a controller function, so I've tried to create a directive to allow me to hook-in to the binding updates, but it doesn't fire when I'd expect it too (only once on page load atm).
The <div>
tag that contains the binding to the SVG has the id='drawContainer'
and is also where I have attached the directive:
<div ng-app="module" class="container-fluid">
<div class="row-fluid" ng-controller="SheetsCtrl">
<div class="span3 well">
<input type="text" ng-model="query" />
<ul class="nav nav-list">
<li class="nav-header" ng-bind-template="Available Labels ({{getLabelCount()}})">Available Labels</li>
<div id="sheetList">
<li ng-repeat="label in labels | filter:query">
<span>
<button class="btn btn-mini btn-danger" ng-click="removeLabel(label)">
<i class="icon-remove-sign"></i>
</button>
<a href="#" ng-click="getSVG(label)">{{label.Name}}</a>
</span>
</li>
</div>
</ul>
</div>
<div class="span9" id="drawContainer" bind-to-svg ng-bind-html-unsafe="selectedLabelDesign"/>
</div>
</div>
However I'm not sure that this is the correct way of doing things, here is the directive with a place-holder for the jQuerySVG code
angular.module('module', []).directive('bindToSvg', function() {
return {
link: function(scope, element, attrs, ctrl) {
var svgDoc = element.children[0];
alert('SVG Changed! (Or not, as the case may be)');
// Attach jQuerySVG to svgDoc and register events.
}
};
});
function SheetsCtrl($scope, $http) {
$scope.labels = [];
$scope.selectedLabelDesign = null;
$scope.labels[0] = {
SVG: '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" /></svg>',
Name: 'Circle'
};
$scope.labels[1] = {
SVG: '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" /></svg>',
Name: 'Rectangle'
};
$scope.getLabelCount = function() {
return $scope.labels.length;
};
$scope.getSVG = function(label) {
var index = $scope.labels.indexOf(label);
$scope.selectedLabelDesign = $scope.labels[index].SVG;
};
$scope.removeLabel = function(label) {
var index = $scope.labels.indexOf(label);
$scope.labels.splice(index, 1);
};
}
If anyone could give me any pointers in the right direction, that'd be great!
I'm not sure what you want it to do when it changes, but you could simply use a $watch to fire whatever it is you need to do.
$scope.$watch('selectedLabelDesign', function(value) {
alert('SVG changed to: ' + value);
});
This watch could be set up either in your controller or your directive. Your call.