首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS指令将数字格式强制到模型

问AngularJS指令将数字格式强制到模型
EN

Stack Overflow用户
提问于 2015-05-31 12:48:23
回答 1查看 647关注 0票数 3

我需要一个带有掩码的输入字段,但我想让模型保持蒙面值。

电话号码应以以下格式以db格式显示和存储:## ## ## ## ##。

我正在使用这个库: Web上的formatter.js,我试着了解在移动应用程序中使用它的最佳方法是什么。

以下是我到目前为止所拥有的:

指令:

代码语言:javascript
复制
.directive('curubaTelephone', function () {
    return {
        restrict: 'AC',
        replace: false,
        link: function (scope, element) {
            element.formatter({
                'pattern': '{{99}} {{99}} {{99}} {{99}} {{99}}',
                'persistent': false
            });
        }
    }
})

HTML:

代码语言:javascript
复制
    <label class="item item-input item-stacked-label">
        <span class="input-label">Fixe</span>
        <input type="text" placeholder="fixe" ng-model="fonction.perso_fixe" class="curubaTelephone">
    </label>

我在index.html中添加了脚本:

代码语言:javascript
复制
<script src="lib/formatter/dist/jquery.formatter.min.js"></script>

控制台返回:

代码语言:javascript
复制
TypeError: element.formatter is not a function
    at link (http://localhost:8100/js/directives.js:48:25)
    at invokeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:16855:9)
    at nodeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:16365:11)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:15714:13)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:15717:13)
    at publicLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:15593:30)
    at $get.boundTranscludeFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:15732:16)
    at controllersBoundTransclude (http://localhost:8100/lib/ionic/js/ionic.bundle.js:16392:18)
    at ngRepeatAction (http://localhost:8100/lib/ionic/js/ionic.bundle.js:33138:15)
    at Object.$watchCollectionAction [as fn] (http://localhost:8100/lib/ionic/js/ionic.bundle.js:22746:13) <input type="text" placeholder="fixe" ng-model="fonction.perso_fixe" class="curubaTelephone ng-pristine ng-untouched ng-valid">
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-31 13:06:11

默认情况下,AngularJS不使用jQuery,只使用其名为jqLite的小子集:

如果没有完整的jQuery,您将无法使用任何jQuery插件(如formatter.js)。

幸运的是,如果您在jQuery本身之前在index.html中包含了AngularJS,那么AngularJS将自动将其用作angular.element (而不是jqLite)。然后,您将能够访问完整的jQuery功能-包括使用其插件的可能性。

代码语言:javascript
复制
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>

更多信息:https://docs.angularjs.org/api/ng/function/angular.element。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30557622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档