代码之家  ›  专栏  ›  技术社区  ›  Chris

基于变量的AngularJS div模板

  •  0
  • Chris  · 技术社区  · 6 年前

    我有一个弹出窗口,我想在其中显示一个模板的基础上,用户选择。

    <misc-Modal visible="showMiscModal" template="{{selectedTemplate}}">
    

    下面是一个示例模板(客户联系人.html):

    <div class="modal fade">
    <div class="modal-dialog my-order-grid-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <span>this is the Customer Contact template</span>
            </div>
        </div>
    </div>
    

            $scope.showCustomerContact = function() {
            alert("showing customer contact");
            $scope.selectedTemplate = "/desktopmodules/mvc/TechSheetMaint/AngularTemplates/CustomerContact.html";
            $rootScope.showMiscModal = true;
        };
    

    我试图通过一个指令来做到这一点:

    angular.module("aps").directive("pmodal",
    function() {
        return {
            restrict: "E",
            transclude: true,
            replace: true,
            scope: true,
            templateUrl: scope.selectedTemplate,
            link: function postLink(scope, element, attrs) {
                scope.$watch(attrs.visible,
                    function(value) {
                        if (value === true)
                            $(element).modal("show");
                        else
                            $(element).modal("hide");
                    });
    
                $(element).on("shown.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = true;
                        });
                    });
    
                $(element).on("hidden.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = false;
                            $scope.showPdfModal=false;
                        });
                    });
            }
        };
    });
    

    ReferenceError:未定义范围

    错误:[$http:badreq文件] http://errors.angularjs.org/1.7.0/ 页面被加载。

    我也尝试了以下建议: Angular.js directive dynamic templateURL

    <misc-Modal visible="$root.showMiscModal"  template-url="selectedTemplate">
    

    angular.module("aps").directive("miscModal",
    function() {
        return {             
            restrict: "E",
            templateUrl: function(elem, attrs) {
                return attrs.templateUrl || "some/path/default.html";
            },
            link: function postLink(scope, element, attrs) {
                scope.$watch(attrs.visible,
                    function(value) {
                        if (value === true)
                            $(element).modal("show");
                        else
                            $(element).modal("hide");
                    });
    
    
                $(element).on("shown.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = true;
                        });
                    });
    
                $(element).on("hidden.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = false;
                            $rootScope.showMiscModal = false;
                        });
                    });
            }
        };
    });
    

    但我在页面加载时遇到了一个错误:

    错误:[$templa任务:tpload] $templaterrequest/tpload?p0=%7B%7SelectedTemplate%7D%7D&p1=404&p2=未找到%20

    templateUrl 取决于我要显示的模板?我已经看过其他的例子了,它们似乎对我不起作用。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Chris    6 年前

    啊。经过反复试验:

    <dmodal visible="$root.showMiscModal">
    
    </dmodal>
    

    模板文件:

    <style>
    @media (min-width: 768px) {
        .modal-dialog {
            width: 800px !important;
            margin: 30px auto;
        }
    }
    

    <span>this is the Status Changes template test4</span>
    <div><span style="float:left;width:150px">Name</span>&nbsp;<span style="float:left; width:150px">Date</span>&nbsp;<span style="float: left;     width:50px">Old</span>&nbsp;<span style="float: left;     width:50px">New</span>&nbsp;Note</div>
    <div ng-repeat="s in statusChanges">
            <span style="float:left;width:150px">{{s.Name}}</span>&nbsp;<span style="float:left; width:150px">{{s.ChangeDate|date:"MM/dd/yyyy HH:mm"}}    </span>&nbsp;<span style="float: left; width:50px">{{s.OldStatus}}</span>&nbsp;<span     style="float: left; width:50px">{{s.NewStatus}}</span>&nbsp;{{s.Note}}
    </div>
    

    指令:

    angular.module("aps").directive("dmodal",
    function($rootScope) {
        return {
           template:'<div class="modal fade">' +
                '<div class="modal-dialog">' +
                '<div class="modal-content">' +
                '<div class="modal-header">' +
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
                "</div>" +
                '<div class="modal-body" ng-include="selectedTemplate"></div>' +
                "</div>" +
                "</div>" +
                "</div>",
    
            restrict: "E",
            transclude: false,
            replace: true,
            scope: true,
            link: function postLink(scope, element, attrs) {
                scope.$watch(attrs.visible,
                    function(value) {
                        if (value === true)
                            $(element).modal("show");
                        else
                            $(element).modal("hide");
                    });
    
                $(element).on("shown.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = true;
                        });
                    });
    
                $(element).on("hidden.bs.modal",
                    function() {
                        scope.$apply(function() {
                            scope.$parent[attrs.visible] = false;
                            $rootScope.showMiscModal=false;
                        });
                    });
            }
        };
    });
    

    $scope.showInternalNotes = function() {
        $rootScope.showMiscModal = true;
        $scope.selectedTemplate = "/desktopmodules/mvc/TechSheetMaint/AngularTemplates/InternalNotes.html?"+Math.floor((Math.random() * 10000000) + 1);
        $scope.getStatusChanges()
    };
    
    $scope.showCustomerContact = function() {
        $rootScope.showMiscModal = true;
        $scope.selectedTemplate= "/desktopmodules/mvc/TechSheetMaint/AngularTemplates/CustomerContact.html?"+Math.floor((Math.random() * 10000000) + 1);
        $scope.getStatusChanges()
    };
    
    $scope.showStatusChanges = function() {
        $rootScope.showMiscModal = true;
        $scope.selectedTemplate = "/desktopmodules/mvc/TechSheetMaint/AngularTemplates/StatusChanges.html?"+Math.floor((Math.random() * 10000000) + 1);
       $scope.getStatusChanges();
    };
    

    阿贾克斯:

        $scope.getStatusChanges = function() {
            const deferred = $q.defer();
            const successFunction = function(response) {
                if (!checkLogin(response, "getStatusChanges")) return;
                $scope.statusChanges = response.data;
                //alert(JSON.stringify(response.data));
                deferred.resolve();
            };
    
            const failureFunction = function(data) {
                console.log(`Error${angular.toJson(data)}`);
                deferred.reject();
            };
            TechSheetFactory.getStatusChanges(successFunction, failureFunction,$scope.TechSheetInfo.WorkOrder.WorkOrderID);
            return deferred.promise;
        };