我刚开始在angular中创建指令,并且对此有点困惑,我想创建一个基本的迷你日历指令来选择给定月份中的日期。请求模板时,我仍然收到错误消息。有什么帮助吗?谢谢
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:
<div ng-app="calendar" ng-controller="calendarController" class="ng-scope">
<mini-calendar type="text" ng-model="config" /> </div>
和模板:
<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>
<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>
您可能会收到此错误https://docs.angularjs.org/error/$compile/tplrt/
当在模板(或templateUrl)上声明了指令并启用了替换模式时,模板必须恰好具有一个根元素。也就是说,模板属性的文本或templateUrl引用的内容必须包含在单个html元素内。
例如,
<p>blah <em>blah</em> blah</p>
不仅仅是简单的<em>blah</em>
等等。否则,替换操作将导致单个元素(指令)被多个元素或节点替换,这在实践中是不受支持的并且通常不需要。
我建议完全避免使用该replace
标志。
现在(1.3.x)已被弃用,并且无论如何都将在下一个主要版本中将其删除:https : //github.com/angular/angular.js/commit/eec6394a342fb92fba5270eee11c83f1d895e9fb
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句