angularjs - 错误:[$compile:multidir] 多个指令 [ngSwitchWhen, ngInclude] 要求包含
问题描述
我需要一个使用 ng-include 预加载数据的解决方案,就像在这篇博文 preloading-data-before-executing-nginclude-in-angularjs中描述的那样。
似乎这个解决方案效果很好,但作者使用 angular 版本 1.0.7。我使用的是 1.2.1 版本,也尝试了 1.7.9 版本,结果是错误消息。
Error: [$compile:multidir] Multiple directives [ngSwitchWhen, ngInclude] asking for transclusion on: <div ng-switch-when="one" bn-preload="oneData" bn-include-log="" ng-include=" 'one.htm' ">
如何用较新的版本修复它,还是有更好的解决方案?
在我的项目中,我通过请求接收数据,并且 ng-include 应该等到请求结束并且要显示的数据可用。
编辑:
我的 HTML 代码是这样的
<div ng-init="validityList = validities.UploadList;" bn-preload="validities" ng-include="'/validity/list'"></div>
我项目中的错误是
Multiple directives [ngInclude, bnPreload] asking for transclusion on
Lemmy 建议的解决方案对我不起作用,因为 bn-preload 和 ng-include 需要在同一个元素上。
编辑2:
我不知道您所说的“bn-preload html”是什么意思。
这是我迄今为止在角度方面所做的。
const BaseApp = angular.module('BaseApp')
.factory('preloader', function ($q, $interval) {
this.load = function (target) {
const deferred = $q.defer();
const loadData = $interval(function () {
if ($q[target].length > 0) {
deferred.resolve($q[target]);
$interval.cancel(loadData);
}
}, 100);
return (deferred.promise);
}
})
.directive('bnPreload', function (preloader) {
function compile(templateElement, templateAttribute, transclude) {
function link($scope, element, attributes) {
let injectedElement = null;
let isDestroyed = false;
preloader.load(attributes.bnPreload).then(
function (preloadedData) {
if (isDestroyed) {
return;
}
$scope.setData(preloadedData);
transclude($scope, function (copy) {
element.after(injectedElement = copy);
});
}
);
$scope.$on(
'$destroy',
function () {
isDestroyed = true;
$(injectedElement).remove();
}
);
}
return (link);
}
return ({
compile: compile,
priority: 250,
terminal: true,
transclude: 'element'
});
})
.controller('validity', function ($scope, $http) {
$http({
method: 'POST',
url: '/validity/getall'
})
.then(function (response) {
$scope.validities = response.data;
});
});
大多数代码与上面链接中的示例相同。
解决方案
推荐阅读
- c# - 在控制台应用程序中使用反射动态创建和调用类
- python - TF-梯度问题
- jquery - 单击获取 data-id 查找行进父 ID 并显示无
- angular - 我将 geofirestore 与 firestore 一起使用,并希望对结果进行分页
- php - 如何在 Google Calendar API PHP 中每 2 天/周/月/年创建一次重复事件
- xcode - Xcode 的“分支”master 是一个不正确的项目
- azure - 来宾用户使用 Azure AD 登录我的应用程序
- php - 如何在特定条件下在表格上添加特定的编辑按钮?
- php - PHP 输出缓冲打印在脚本末尾
- php - 如何在 amCharts 中更改甘特图的高度?