为什么在链接函数中加载日期时绑定到指令中创建的html?

时间:2016-08-15 10:20:56

标签: javascript angularjs directive

我想为重复代码编写指令,例如在此示例中我想从attachmentUsageService加载数据,然后创建html。对于第一步服务加载数据成功但该数据不绑定到创建的html元素。 假设下面的指令代码

  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);
                    }));
            };
        }
    };
}]);

为什么按钮的文字不是例如&#34;测试[编号]&#34;点击它后?

1 个答案:

答案 0 :(得分:1)

attachments是一个局部变量,因此您的视图无法使用它。将var attachments = []更改为$scope.attachments = [],它应该在您的视图中可用。