我有一个外部控制器,它在其视图中包含一个指令。该指令获取流程点列表并生成链接,您可以在其中选择每个链接。它在链接功能中正确设置了 HTML,但链接的 ng-click 操作不起作用。
有任何想法吗?:)
非 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);
}
}
}
});