Plunker
I have an external controller that contains a directive inside my view. The directive receives a list of process points and creates links where you can select them. It correctly configures HTML in the link function, but ng-click link actions do not work.
Any ideas? :)
Code for Non-Plunkering
HTML
<!DOCTYPE html> <html> <head><link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"> <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <script src="script.js"></script> </head> <body ng-app="app"> <div ng-controller="widget"> <process stages="production" at="productionAt"></process> </div> </body> </html>
Js
angular.module('app', ['app.directives', 'app.controllers']); angular.module('app.controllers', []) .controller('widget', function($scope) { var selectStage = function () { alert(this.label + " selected."); $scope.processAt = this; } $scope.production = [ {label: "Starting", select: selectStage} , {label: "Fermenting", select: selectStage} , {label: "Pouring", select: selectStage} , {label: "Beer!", select: selectStage} ]; $scope.productionAt = $scope.production[0]; }); angular.module('app.directives', []) .directive('process', function() { return { restrict: 'E' , replace: true , template: '<ol class="nav nav-pills"></ol>' , scope: { stages: "=" , at: "=" } , link: function postLink(scope, element, attrs) { for (var i = 0; i < scope.stages.length; i++) { var $stage = $('<li ng-click="stages['+i+'].select()"><a>'+scope.stages[i].label+'</a></li>'); if (scope.at == scope.stages[i]) { $stage.addClass('active'); } $(element).append($stage); } } } });
source share