Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度模式中自定义属性指令的刷新元素_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 角度模式中自定义属性指令的刷新元素

Javascript 角度模式中自定义属性指令的刷新元素,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个自定义的角度指令,我用它来改变商品上价格的显示。它存储为年度值,我需要在允许用户以年度或月度查看/编辑之间切换。因此,我的自定义指令正在处理输入框(不能将其用于标签、div、span,但这是另一个问题) 我可以看到,当我切换属性时,它会拾取更改,但输入中的值不会更改,我假设这是因为没有重新渲染元素。我有没有办法强迫你这么做 这是我的指令 angular.module('common.directive').directive('priceMonthly', function() {

我有一个自定义的角度指令,我用它来改变商品上价格的显示。它存储为年度值,我需要在允许用户以年度或月度查看/编辑之间切换。因此,我的自定义指令正在处理输入框(不能将其用于标签、div、span,但这是另一个问题)

我可以看到,当我切换属性时,它会拾取更改,但输入中的值不会更改,我假设这是因为没有重新渲染元素。我有没有办法强迫你这么做

这是我的指令

angular.module('common.directive').directive('priceMonthly', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            var perMonth = false;

            attr.$observe('priceMonthly', function(isMonthly) {
                perMonth = (isMonthly.toLowerCase() === "true");
                console.log("per month is " + perMonth); //updating perMonth correctly at this point
                //guessing I need to do something here or in the function that changes the value of the attribute
            });

            function fromView(value){
                return perMonth ? value * 12 : value;
            }

            function toView(value){
                return perMonth ? value / 12 : value;
            }

            ngModel.$parsers.push(fromView);
            ngModel.$formatters.push(toView);
        }
    };
});
这就是我使用它的地方

<input type="text" price-monthly="{{monthlySqftView}}"
       class="form-control no-animate" name="item.priceLow"
       ng-model="item.priceLow" ui-money-mask="2"/>

下面是我目前所处位置的一部分。因此,如果下拉列表是按月计算的,我输入12,那么价格是正确的144,这将是全年价格。现在如果你把下拉列表改为按年,我需要输入更新为144。
@rschlachter,我不确定这是否能帮到你。AngularJS使用scope.digest()清除脏数据。您可能需要将这行代码放入您的方法中

        attr.$observe('priceMonthly', function(isMonthly) {
            perMonth = (isMonthly.toLowerCase() === "true");
            console.log("per month is " + perMonth); //updating perMonth correctly at this point
            //guessing I need to do something here or in the function that changes the value of the attribute
            //put it here, if you instantiated scope already. 
            scope.digest();
            //scope.apply();
        });

希望它能起作用,我不确定这是否能帮到你。AngularJS使用scope.digest()清除脏数据。您可能需要将这行代码放入您的方法中

        attr.$observe('priceMonthly', function(isMonthly) {
            perMonth = (isMonthly.toLowerCase() === "true");
            console.log("per month is " + perMonth); //updating perMonth correctly at this point
            //guessing I need to do something here or in the function that changes the value of the attribute
            //put it here, if you instantiated scope already. 
            scope.digest();
            //scope.apply();
        });

希望它能工作

尝试
范围。$apply()
在您做了必要的更改后

尝试
范围。$apply()
在您做了必要的更改后

似乎您想做这样的事情

和JS指令

angular.module('common.directive').directive('priceMonthly', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            var perMonth = false;

            attr.$observe('priceMonthly', function(isMonthly) {
                perMonth = (isMonthly.toLowerCase() === "true");
                console.log("per month is " + perMonth); //updating perMonth correctly at this point
                //guessing I need to do something here or in the function that changes the value of the attribute
            });

            function fromView(value){
                return perMonth ? value * 12 : value;
            }

            function toView(value){
                return perMonth ? value / 12 : value;
            }

            ngModel.$parsers.push(fromView);
            ngModel.$formatters.push(toView);
        }
    };
});

我希望这能对你有所帮助。

看来你想做这样的事

和JS指令

angular.module('common.directive').directive('priceMonthly', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {
            var perMonth = false;

            attr.$observe('priceMonthly', function(isMonthly) {
                perMonth = (isMonthly.toLowerCase() === "true");
                console.log("per month is " + perMonth); //updating perMonth correctly at this point
                //guessing I need to do something here or in the function that changes the value of the attribute
            });

            function fromView(value){
                return perMonth ? value * 12 : value;
            }

            function toView(value){
                return perMonth ? value / 12 : value;
            }

            ngModel.$parsers.push(fromView);
            ngModel.$formatters.push(toView);
        }
    };
});

我希望这会对您有所帮助。

您可以发布HTML(即使用该指令的HTML)吗?您可以在上找到它吗?您可以发布HTML(即使用该指令的HTML)吗?您可以在上找到它,但我得到了scope.digest()不是一个函数。尝试了作用域。$digest(),但出现以下错误。尝试了此操作,但我得到了scope.digest()不是函数。尝试了作用域。$digest(),但出现以下错误。尝试了这个,我得到了以下错误。删除作用域。$apply()并将名称atribute添加到具有相同名称ng model的输入框中,然后重试。尝试此操作后,出现以下错误。删除作用域。$apply()并将名称atribute添加到与ng model同名的输入框中,然后重试。不完全是这样。我已经更新了我的原始帖子,加入了我目前根据你的JSFIDDLE所做的事情。如果下拉列表是每月一次的,我输入12,价格需要在输入中更新吗?也许,你需要这个?不完全需要。我已经更新了我的原始帖子,加入了我目前根据你的JSFIDDLE所做的事情。如果下拉列表是每月一次的,我输入12,价格需要在输入中更新吗?也许,你需要这个?