在angular js中通过ajax调用加载模板url

load template url by ajax call in angular js

本文关键字:加载 调用 url ajax angular js      更新时间:2023-09-26

我想发送一个ajax请求,为modal的templateurl获取jsp/html页面。我用这样的代码写的。

 var modalInstance = $uibModal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: 'pages/recordingDetailPopup .jsp',
                    controller: 'FileDetailCtrl',
                    size: 'lg',
                    resolve: {
                        activeRecords: function () {
                            return inbnoxList;
                        }
                    }
                });

但我想做一些类似的事情

 var modalInstance = $uibModal.open({
                animation: $scope.animationsEnabled,
                templateUrl: function(){
                $http.get('fileDetailJsp');
                },
                controller: 'FileDetailCtrl',
                size: 'lg',
                resolve: {
                    activeRecords: function () {
                        return inbnoxList;
                    }
                }
            });

如何实现此功能。请提出建议。

第二个代码段中的

templateUrl将返回promise,而不是jsp文件的响应。

在上面的场景中,您需要首先从jsp获得响应,然后在下面的模式-上调用.open方法

$http.get('fileDetailJsp').then(function(url){
      var modalInstance = $uibModal.open({
            animation: $scope.animationsEnabled,
            templateUrl: url,
            controller: 'FileDetailCtrl',
            size: 'lg',
            resolve: {
                activeRecords: function () {
                    return inbnoxList;
                }
            }
        });
}, function(){
  // error here
 });

希望这能帮助。。。

当您调用$http.get('fileDetailJsp');}时,它会返回一个Promise。当你打电话给$uibModal.open时,承诺还没有准备好。这就是为什么你一无所获。正如@pdenes所提到的,你需要这样的东西:

$http.get('fileDetailJsp').then(function(response) {
   $uibModal.open(...use something from response...); 
});