angularjs - 带有 ngModelController 格式化程序的 Angular 指令

标签 angularjs angularjs-directive angular-ngmodel

我正在尝试实现一个简单的指令,将格式化程序添加到 <input type="date"> field 。该指令如下所示:

myApp.directive("date-format", function (dateFilter) {
    return {
        restrict: "A",
        require: "ngModel",
        link: function (scope, element, attributes, ngModelController) {
            ngModelController.$formatters.push(function (modelValue) {
                return modelValue && new Date(modelValue);
            });
        }
    };
});

它运行得很好,直到我将其名称更改为 mb-date-format 。我创建了一个 JS fiddle :http://jsfiddle.net/HB7LU/4458/ 。它似乎可以正常使用像 aaa 这样的名称。 , bbb , ccc依此类推,但当您将名称更改为 jjj 时,它会停止工作, zzz ,……

最佳答案

尝试将指令名称驼峰式命名为“dateFormat”,例如:

myApp.directive("dateFormat", function (dateFilter) 


然后在 html 中使用它,如下所示

<input type="date" ng-model="date" date-format>

关于angularjs - 带有 ngModelController 格式化程序的 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24396636/

相关文章:

javascript - ajax请求时禁用按钮

javascript - 将 AngularJS 数组显示为 'human' 可读字符串

javascript - 指令中定义的 Ng-click 未触发

javascript - 使用 Karma 在 play 2.2.x 应用程序中测试 javascript

javascript - 为什么 $watch 中的监听函数没有被触发?

javascript - 在其他 javascript 框架模板中运行 Angular 指令

javascript - 出现错误 : "Can' t bind to 'ngModal' since it isn't a known property of 'input' "

javascript - 在评论中使用 AngularJS 指令有什么意义?

javascript - 获取TextArea angularjs的值(value)

angularjs - ng-show 中 $index 的可访问性