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?

Omital
  • 212
  • 2
  • 12

1 Answers1

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.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • Another question: Can I create html in link function and have binding in it and then return it? – Omital Aug 15 '16 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 '16 at 10:58
  • Here is a nice thread.... http://stackoverflow.com/questions/9381926/angularjs-insert-html-into-view – Thalaivar Aug 15 '16 at 10:59