【问题标题】:What is causing "Error: $compile:tplrt Invalid Template Root" while the template have only one root element是什么导致了“错误:$compile:tplrt Invalid Template Root”,而模板只有一个根元素
【发布时间】:2016-04-23 12:54:09
【问题描述】:

我正在尝试让 uibmodal 工作,但出现以下错误。

“指令'uibModalBackdrop'的模板必须只有一个根元素。uib/template/modal/backdrop.html”

“指令'uibModalWindow'的模板必须只有一个根元素。uib/template/modal/window.html”

而模板只有一个根元素。

这是应该加载模式的页面:

<div class="col-md-8">
<div><h1>Categorien</h1>
    <div class="row">
        <div class="col-md-9"></div>
        <div class="col-md-2">
            <button type="button" class="btn btn-success btn-lg" ng-click="ctrl.openUpdateModal()">Niewe Categorie</button>
        </div>
        <div class="col-mid-1"></div>
    </div>
    <div>
        <table class="table table-striped">
            <thead >
                <tr>
                    <th colspan="1">id</th>
                    <th colspan="1">name</th>
                    <th colspan="1">remove</th>
                    <th colspan="1">edit</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="category in ctrl.categories">
                    <td>{{category.id}}</td>
                    <td>{{category.name}}</td>
                    <td><img src="public/images/no.png"></td>
                    <td><img src="public/images/edit1.png"></td>
                </tr>

            </tbody>
        </table>
    </div>
</div>

这是该页面的控制器:

(function(){
var module = angular.module('catalog');

module.controller('CategoryController', CategoryController);

CategoryController.$inject = ['CategoryService', '$uibModal'];

function CategoryController(CategoryService, $uibModal) {

    var vm = this;

    vm.categories = [];

    vm.addCategory = addCategory;
    vm.openUpdateModal = openUpdateModal;
    vm.updateCategory = updateCategory;
    vm.deleteCategory = deleteCategory;

    refresh();

    function refresh(){
        // CategoryService.getCategories().then(function(categories){
            vm.categories = [
            {id: 01, name: 'Something'},
            {id: 02, name: 'Something'},
            {id: 03, name: 'Something'},
            {id: 04, name: 'Something'}];
        // });
    }

    function addCategory(category){
        CategoryService.addCategory(category).then(function(){
            vm.categories = {};
            refresh();
        });
    }

    function openUpdateModal() {
        var instance = $uibModal.open({
            templateUrl: "/modules/catalog/template/category-create-modal.html",
            controller: 'CategoryModalController',
            controllerAs: 'ctrl'
        })

    }

    function updateCategory(category) {
        CategoryService.updateCategory(category).then(function(){
            refresh();
        })
    }


    function deleteCategory(category) {
        CategoryService.deleteCategory(category).then(function(){
            refresh();
        })
    }
}

}).call(this);

这是模态的控制器:

(function(){
var module = angular.module('catalog');

module.controller('CategoryModalController', CategoryModalController);

CategoryModalController.$inject = ['$uibModalInstance'];

function CategoryModalController($uibModalInstance){
    var vm = this;
    vm.ok = function () {
        $uibModalInstance.close(vm.selected.item);
    };

    vm.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };

}
}).call(this);

【问题讨论】:

  • 我认为这是this question的副本
  • 我尝试了他们提供的解决方案,但没有奏效。
  • 很抱歉打断你,但你最后错过了一个&lt;/div&gt;
  • 我让它以 div 的方式工作,并且有人在另一个问题的 cmets 中说了什么,谢谢大家的帮助。

标签: html angularjs angular-ui-bootstrap


【解决方案1】:

您的 HTML 无效,您没有关闭 col-md-8。这是正确的 HTML:

<div class="col-md-8">
    <div>
        <h1>Categorien</h1>
        <div class="row">
            <div class="col-md-9"></div>
            <div class="col-md-2">
                <button type="button" class="btn btn-success btn-lg" ng-click="ctrl.openUpdateModal()">Niewe Categorie</button>
            </div>
            <div class="col-mid-1"></div>
        </div>
        <div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th colspan="1">id</th>
                        <th colspan="1">name</th>
                        <th colspan="1">remove</th>
                        <th colspan="1">edit</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="category in ctrl.categories">
                        <td>{{category.id}}</td>
                        <td>{{category.name}}</td>
                        <td><img src="public/images/no.png"></td>
                        <td><img src="public/images/edit1.png"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

【讨论】:

  • 您确定您现在没有收到其他错误吗?
  • 可能是模板被缓存了?清除缓存或在隐身模式下测试
  • 好主意。 @OussamaZorgui 您应该尝试清除缓存或更改模板文件名。
  • 好的,问题已经解决,再次感谢您的帮助。
【解决方案2】:

如果你使用 angular-bootstrap,你需要加载脚本模板,angular-bootstrap/ui-bootstrap-tpls.min.js

例如

<script src="/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 2017-02-02
    • 2011-08-13
    相关资源
    最近更新 更多