我有以下指令正常工作:
app.directive('theDirective', ['musicInfoService',
function(musicInfoService) {
return {
restrict: 'A',
scope: { position: '@', last: '@', release: '=',
artist: '=', versions: '@' },
link: function(scope, element, attrs) {
element.bind('click', function() {
// Highlight clicked element
angular.element(document.querySelector('.clicked')).removeClass('clicked');
element.addClass('clicked');
// Create the collapse element or select existing one
var collapseQuery = document.querySelector('#collapse');
var collapse = collapseQuery === null ?
angular.element('<div id="collapse" ng-controller="VersionController" class="col-md-12 col-xs-12"> \
<div class="inner"> \
<ul> \
<li class="title">{{release.title}}</li> \
<li class="row top" ng-show="versions"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
<div class="col-md-2">Country</div> \
<div class="col-md-2">Cat. Nº</div> \
</li> \
<li class="row" ng-show="versions" ng-repeat="version in versions | filter: \'!file\' | orderBy: version.released"> \
<div class="col-md-1">{{(version.released | release:4) || \'-----\'}}</div> \
<div class="col-md-3">{{version.format}}</div> \
<div class="col-md-3">{{version.label}}</div> \
<div class="col-md-2">{{version.country}}</div> \
<div class="col-md-2">{{version.catno}}</div> \
</li> \
<li class="row top" ng-hide="!release.format"> \
<div class="col-md-1">Year</div> \
<div class="col-md-3">Format</div> \
<div class="col-md-3">Label</div> \
</li> \
<li class="row" ng-hide="!release.format"> \
<div class="col-md-1">{{release.year}}</div> \
<div class="col-md-3">{{release.format}}</div> \
<div class="col-md-8">{{release.label}}</div> \
</li> \
</ul> \
</div> \
</div>') :
angular.element(collapseQuery);
// Based on the position of the clicked element calculate the rounded number up to the nearest multiple of four
var calculatedPosition = Math.ceil(scope.position / 4) * 4;
// Get the element at the calculated position or the last one
var calculatedQuery = document.querySelector('[position="' + calculatedPosition + '"]');
if (calculatedQuery === null) calculatedQuery = document.querySelector('[last="true"]');;
var calculatedElement = angular.element(calculatedQuery);
// Insert the collapse element after the element at the calculated position
calculatedElement.after(collapse);
// Highlight the calculated element
angular.element(document.querySelector('.calculated')).removeClass('calculated');
calculatedElement.addClass('calculated');
});
scope.$on('$destroy', function() {
element.unbind('click');
});
}
};
}
]);
问题在于我无法找到一种方法来集成必要的功能来填充element.bind上的指令中的字段(&#39;点击&#39;)。
该功能如下所示:
var versions = scope.release.format;
musicInfoService.getReleaseVersions(scope.release.id)
.success(function(data) {
if (data.error) return;
var format = data.versions,
formats = scope.release.format;
if (format !== '') {
formats = format;
}
scope.versions = formats;
})
scope.release.id和scope.release.format来自另一个$ http调用。
这里用a Plunker来说明错误(有问题的函数在directive.js的第49行中被注释掉了。)
任何指针?