2

I want to write directive for repetitive code for example in this sample I want load data from attachmentUsageService and then create html. for first step service load data successfully but this data is not bind to created html element. Suppose below code for directive

  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 button's text is not for example "Test [number]" after click on it?

1 Answer 1

1

attachments is a local Variable, so its not available to your view. Change var attachments = [] to $scope.attachments = [], it should be available in your view.

3
  • Another question: Can I create html in link function and have binding in it and then return it?
    – Omital
    Aug 15, 2016 at 10:56
  • 1
    It would be better to go with template or templateUrl for html, link function would have a instance of your template and not source... in that case you have to use compile function where you can get the exact source template...
    – Thalaivar
    Aug 15, 2016 at 10:58
  • Here is a nice thread.... stackoverflow.com/questions/9381926/…
    – Thalaivar
    Aug 15, 2016 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.