Angularjs 访问指令';s控制器函数内部链接函数

Angularjs 访问指令';s控制器函数内部链接函数,angularjs,angularjs-directive,Angularjs,Angularjs Directive,如何访问链接中的控制器功能?这将返回错误: ReferenceError:未定义GetFormattedValue 看到了一些示例,其中一个通过require注入一个单独的控制器,并且能够通过注入它来访问控制器,如下面我的代码中所示,作为link函数中的第四个参数。但是,我需要这个控制器在这个指令内 .directive('testDirective', function () { link: function ($scope, element, attribute, controlle

如何访问链接中的控制器功能?这将返回错误: ReferenceError:未定义GetFormattedValue

看到了一些示例,其中一个通过require注入一个单独的控制器,并且能够通过注入它来访问控制器,如下面我的代码中所示,作为link函数中的第四个参数。但是,我需要这个控制器在这个指令内

.directive('testDirective', function () {
    link: function ($scope, element, attribute, controller) {

        attribute.$observe('numbers', function(value) { 
            controller.setNumbers(value);
        });

        attribute.$observe('decimals', function(decimals) { 
            decimals = decimals || defaultValue();
            decimals = (decimals.length > 2 ? decimals.slice(0, 2 - decimals.length) : decimals);
            $scope.decimals = controller.getFormatedValue(decimals, 'decimals');
        });

    },
    controller: function ($scope, $element, $attrs) {

        this.setNumbers = function (numbers) {
            numbers = numbers || $scope.defaultValue();
            $scope.numbers = getFormatedValue(numbers, 'numbers');
        };

        this.getFormatedValue = function(value, kindofvalue){
            var maxDecimals = '2',
                    returnValue;

            if (kindofvalue === 'decimals'){
                returnValue = addzero(value, maxDecimals);
            }
            else if (kindofvalue === 'numbers'){
                    returnValue = value.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
            }
            return returnValue;
        };

        this.defaultValue = function() {
            return '0';
        };

        this.addzero = function(decimalValue , maxDecimals) {
            return decimalValue.length < maxDecimals ? $scope.addzero(decimalValue + '0', maxDecimals) : decimalValue;
        };
    }
};
.directive('testDirective',function(){
链接:函数($范围、元素、属性、控制器){
属性。$observe('numbers',函数(值){
控制器。设置编号(值);
});
属性。$observe('decimals',函数(decimals){
小数=小数| |默认值();
小数=(小数长度>2?小数切片(0,2-小数长度):小数);
$scope.decimals=controller.getFormattedValue(decimals,'decimals');
});
},
控制器:函数($scope、$element、$attrs){
this.setNumbers=函数(数字){
数字=数字| |$scope.defaultValue();
$scope.numbers=GetFormattedValue(数字,'numbers');
};
this.getFormattedValue=函数(value,kindofvalue){
变量maxDecimals='2',
返回值;
如果(kindofvalue==='decimals'){
returnValue=addzero(值,最大小数);
}
else if(kindofvalue==='numbers'){
returnValue=value.replace(/\B(?=(\d{3})+(?!\d))/g';
}
返回值;
};
this.defaultValue=函数(){
返回“0”;
};
this.addzero=函数(小数值,最大小数){
返回decimalValue.length
在指令定义中添加
require:'testDirective'
,即:

.directive('testDirective', function () {
    return {
        require: 'testDirective',
        link: function ($scope, element, attribute, controller) {
            ...
        },
        controller: function ($scope, $element, $attrs) {
            ...
        }
    };
});
解决了。无需添加“require:'testDirective”,但这些让我明白了。代码确实有效,但控制器中的函数内部存在引用问题。函数“addzero”没有通过此引用。我收到的反馈不是很清楚,所以我认为我无法在隔离范围内访问控制器。