How can I write a directive to handle repetitive code? In this example, I need to load data from attachmentUsageService and generate HTML. The service successfully loads the data in the first step, but the data is not bound to the created HTML element. See the directive code below:
app.directive('mySharedScope', ["abp.services.app.attachmentUsage", function (attachmentUsageService) {
return {
restrict: 'AE',
template: ' <button ng-click="open()">Test {{attachments.length}}</button><div>',
scope: { },
link: function ($scope, $element, $attrs) {
var attachments = [];
$scope.open = function () {
var _objectType = 0;
var _objectId = $attrs.objectId;
if ($attrs.objectType == 'person')
_objectType = 1;
if ($attrs.objectType == 'company')
_objectType = 2;
abp.ui.setBusy(null,
attachmentUsageService.getObjectAttachments({ objectId: _objectId, objectType: _objectType, itemCount: 10 }).success(function (data) {
attachments= data.attachments;
alert(attachments.length);
}));
};
}
};
}]);
Why is the button's text not showing "Test [number]" after clicking on it?