如何将控制器绑定到指令?

时间:2019-07-09 05:38:36

标签: angularjs

我使用伪指令将数字格式化为货币,直到我将模板与伪指令分离为止,它一直可以正常工作。现在它不起作用了。

指令

angular.module('SvUtilities')
.directive('numberInput', function ($filter) {    
    return {
        require: 'ngModel',
        link: function (scope, elem, attrs, ngModelCtrl) {
            ngModelCtrl.$formatters.push(function (modelValue) {
                return setDisplayNumber(modelValue, true);
            });
            elem.bind('keyup focus', function () {
                setDisplayNumber(elem.val());
            });

            function setDisplayNumber(val, formatter) {
                var valStr, displayValue;
                if (typeof val === 'undefined') {
                    return null;
                }
                valStr = val.toString();
                displayValue = valStr.replace(/,/g, '').replace(/[A-Za-z]/g, '');

                displayValue = parseFloat(displayValue);

                displayValue = (!isNaN(displayValue)) ? displayValue.toString() : '';



                // handle leading character -/0

                if (valStr.length === 1 && valStr[0] === '-') {

                    displayValue = valStr[0];

                } else if (valStr.length === 1 && valStr[0] === '0') {

                    displayValue = '';

                } else {

                    displayValue = $filter('number')(displayValue);

                }





                if (attrs.positive && displayValue[0] === '-') {

                    displayValue = displayValue.substring(1);

                }



                if (typeof formatter !== 'undefined') {

                    return (displayValue === '') ? 0 : displayValue;

                } else {

                    elem.val((displayValue === '0') ? '' : displayValue);

                }

            }



            function setModelNumber(val) {

                var modelNum = val.toString().replace(/,/g, '').replace(/[A-Za-z]/g, '');

                modelNum = parseFloat(modelNum);

                modelNum = (!isNaN(modelNum)) ? modelNum : 0;

                if (modelNum.toString().indexOf('.') !== -1) {

                    modelNum = Math.round((modelNum + 0.00001) * 100) / 100;

                }

                if (attrs.positive) {

                    modelNum = Math.abs(modelNum);

                }

                return modelNum;

            }

        },

        restrict: "E",

        replace: true,

        templateUrl: "../svAngularLib/modules/utilities/directives/currencyDir.html",

        scope: {

            cssColor: '=',

            onChange: '&',

        },

        controllerAs: 'vm',

        bindToController: true

    };

});

脚本

<div>
    <input maxlength="16" type='text' number-input ng-model="vm.currencyVal"
           value="vm.currencyVal" ng-change="" ng-class="" />    
    <span class="input-group-addon">₪</span>
</div>

模板

{{1}}

键入数字时,应添加逗号分隔符,我也想为其添加一些功能(ng-change)或专有属性(CSS类)

1 个答案:

答案 0 :(得分:0)

它在指令上缺少属性controller

app.controller('SomeController', function () {
  ...
});

app.directive('directiveName', function () {
  return {
    ...
    controller: 'SomeController',
    ...
  };
});