无法在指令中设置undefined的属性

时间:2019-05-14 11:00:11

标签: angularjs

我的目标是使用数字十进制2管道过滤ngModel值,这是无法从cshtml中获得的

我使用下面的博客示例创建自定义指令: https://blog.krusen.dk/angularjs-decimals-browsers/

我遇到错误:

  

[SFC错误]无法设置未定义的属性'$ render'

我不想检查NgModel未定义,因为它总是应该存在,但是为什么我在指令中得到ngModel未定义

app.directive("decimals", function ($filter) {
    return {
        restrict: "A", // Only usable as an attribute of another HTML element
        require: "?ngModel",
        scope: {
            decimals: "@",
            decimalPoint: "@"
        },
        link: function (scope, element, attr, ngModel) {
            var decimalCount = parseInt(scope.decimals) || 2;
            var decimalPoint = scope.decimalPoint || ".";

            // Run when the model is first rendered and when the model is changed from code
            ngModel.$render = function() {
                if (ngModel.$modelValue != null && ngModel.$modelValue >= 0) {
                    if (typeof decimalCount === "number") {
                        element.val(ngModel.$modelValue.toFixed(decimalCount).toString().replace(".", ","));
                    } else {
                        element.val(ngModel.$modelValue.toString().replace(".", ","));
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

我得到了答案 Get value from ngModel in custom directive

我更改了NGModel参数的顺序

相关问题