移动到其他页面时,自定义指令不起作用

时间:2017-04-02 10:41:10

标签: angularjs angularjs-directive

我在一个页面上有一个简单的setcalender指令,它工作正常。但是,如果我转到另一页然后再回来,压延机就不再显示了。以下是指令代码。我在渲染之前是否缺少清理范围或什么?

HTML代码

<div>
    <label>Date</label>
    <input type="text" ng-model="date" setcalendar>
</div>

自定义指令

angular.module("myapp").directive("setcalendar", function () {
    debugger;
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {},
        link: function (scope, element, attrs) {

            scope.$watch(attrs.datepicker, function () {
                $(element).datepicker({
                    dateFormat: 'mm/dd/yy',
                    mode: 'single',
                    changeMonth: true,
                    changeYear: true,
                    showOn: "button",
                    buttonImage: "Content/images/Cal.png",
                    buttonImageOnly: true,
                    class:"calendar",
                    onSelect: function (dateText) {
                        var expression = attrs.ngModel + " = " +
                           "'" + dateText + "'";
                        scope.$apply(expression);

                    }
                });
            });
        }
    }
});

0 个答案:

没有答案