简体   繁体   English

从templateUrl检索AngularJS指令问题模板

[英]AngularJS Directive Issue retrieving template from templateUrl

I'm new to creating directives in angular, and I'm kind of stuck here, I want to create a basic mini calendar directive to select a date in a given month. 我刚开始使用angular创建指令,我对此有些困惑,我想创建一个基本的迷你日历指令来选择给定月份中的日期。 I'm still getting error from the template when it's requested. 请求模板时,我仍然收到错误消息。 Any help? 有什么帮助吗? Thanks 谢谢

 function calendarController($scope) { $scope.config = new Date(2000, 0, 1); } angular.module("calendar", []).directive('miniCalendar', function($parse) { return { restrict: "E", replace: true, templateUrl: "../views/template.html", transclude: true, controller: mCalBinding, compile: function(element, attrs) { debugger; var modelAccessor = $parse(attrs.ngModel); return function(scope, element, attrs, controller) { var processChange = function() { // var date = new Date(element.datepicker("getDate")); scope.$apply(function(scope) { // Change bound variable debugger; modelAccessor.assign(scope, date); }); }; element.datepicker({ inline: true, onClose: processChange, onSelect: processChange }); // scope.$watch(modelAccessor, function(val) { // return true; // }); }; } }; }); function mCalBinding($scope) { //Binding of the directive $scope.currentDate = new Date(2000, 0, 1); $scope.prev = function(data) { }; $scope.next = function(data) { }; $scope.currentMonth = 'December'; $scope.currentYear = '2679'; $scope.days = [{ day: "1" }, { day: "2" }, { day: "3" }, { day: "4" }, { day: "5" }, { day: "6" }, { day: "7" }, { day: "8" }, { day: "9" }, { day: "10" }, { day: "11" }, { day: "12" }, { day: "13" }]; $scope.weeks = [{ week: "1", days: [{ day: "1" }, { day: "2" }, { day: "3" }, { day: "4" }, { day: "5" }, { day: "6" }, { day: "7" }, { day: "8" }, { day: "9" }, { day: "10" }, { day: "11" }, { day: "12" }, { day: "13" }] }, { week: "2", days: [{ day: "1" }, { day: "2" }, { day: "3" }, { day: "4" }, { day: "5" }, { day: "6" }, { day: "7" }, { day: "8" }, { day: "9" }, { day: "10" }, { day: "11" }, { day: "12" }, { day: "13" }] }, { week: "3", days: [{ day: "1" }, { day: "2" }, { day: "3" }, { day: "4" }, { day: "5" }, { day: "6" }, { day: "7" }, { day: "8" }, { day: "9" }, { day: "10" }, { day: "11" }, { day: "12" }, { day: "13" }] }, { week: "4", days: [{ day: "1" }, { day: "2" }, { day: "3" }, { day: "4" }, { day: "5" }, { day: "6" }, { day: "7" }, { day: "8" }, { day: "9" }, { day: "10" }, { day: "11" }, { day: "12" }, { day: "13" }] }]; } /* Fragment End - directive */ 

HTML: HTML:

<div ng-app="calendar" ng-controller="calendarController" class="ng-scope">
    <mini-calendar type="text" ng-model="config" /> </div>

and the template: 和模板:

<input type="text" ng-bind="currentDate">
<div >
    <div >
        <a>
            <span ng-click="prev">Prev</span>
        </a>
        <a>
            <span ng-click="next">Next</span>
        </a>
        <div >
            <span ng-bind="currentMonth">January</span>&nbsp;
            <span ng-bind="currentYear">2000</span>
        </div>
    </div>
    <table>
        <thead>
            <tr>
                <th ng-repeat="day in days">
                    <span ng-bind="day"></span>
                </th>
            </tr>
        </thead>
        <tbody>
        <tr ng-repeat="week in weeks">
            <td ng-repeat="day in days">
                <a ng-bind="day" ng-click="selectDate">1</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>

You probably get this error https://docs.angularjs.org/error/$compile/tplrt/ 您可能会收到此错误https://docs.angularjs.org/error/$compile/tplrt/

When a directive is declared with template (or templateUrl) and replace mode on, the template must have exactly one root element. 如果在模板(或templateUrl)上声明了指令并启用了替换模式,则模板必须恰好具有一个根元素。 That is, the text of the template property or the content referenced by the templateUrl must be contained within a single html element. 也就是说,模板属性的文本或templateUrl引用的内容必须包含在单个html元素中。

For example, <p>blah <em>blah</em> blah</p> instead of simply blah <em>blah</em> blah. 例如, <p>blah <em>blah</em> blah</p>而不是简单的blah <em>blah</em> blah。 Otherwise, the replacement operation would result in a single element (the directive) being replaced with multiple elements or nodes, which is unsupported and not commonly needed in practice. 否则,替换操作将导致单个元素(指令)被多个元素或节点替换,这在实践中是不受支持的,通常不需要。

What I recommend is to avoid using the replace flag at all. 我建议完全避免使用replace标志。

It is now (1.3.x) deprecated and will be removed in the next major version anyway: https://github.com/angular/angular.js/commit/eec6394a342fb92fba5270eee11c83f1d895e9fb 现在(1.3.x)已被弃用,并且无论如何都将在下一个主要版本中将其删除: https : //github.com/angular/angular.js/commit/eec6394a342fb92fba5270eee11c83f1d895e9fb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM