为什么在 link 函数中加载的日期未绑定到在指令中创建 html?
Why date loaded in link function nit binded to created html in 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);
}));
};
}
};
}]);
为什么点击后按钮的文本不是 "Test [number]"?
attachments
是局部变量,因此您的视图不可用。将 var attachments = []
更改为 $scope.attachments = []
,它应该在您的视图中可用。
我想为示例中的重复代码编写指令,例如我想从 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);
}));
};
}
};
}]);
为什么点击后按钮的文本不是 "Test [number]"?
attachments
是局部变量,因此您的视图不可用。将 var attachments = []
更改为 $scope.attachments = []
,它应该在您的视图中可用。