我想为重复代码编写指令,例如在此示例中,我希望从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);
}));
};
}
};
}]);
为什么单击按钮后的文本不是例如“测试[数字]”?
最佳答案
attachments
是局部变量,因此您的视图不可用。将var attachments = []
更改为$scope.attachments = []
,它在您的视图中应该可用。