【问题标题】:How to update value of ng-model with the expression in html part如何使用 html 部分中的表达式更新 ng-model 的值
【发布时间】:2014-07-18 15:48:29
【问题描述】:

为什么 ng-model 的值不随表达式更新。在 ng-model 定义之前,值得到更新

值将在第 2 阶段或第 3 阶段发生变化时立即更新

<input type="text" name="phase1" value="{{phase2 - phase3}}" ></input>

值不会更新

<input type="text" name="phase1" value="{{phase2 - phase3}}" ng-model="phase1"></input>

所以我想写一个指令来评估指令中的表达式并将输出更新为模型,

这是 html 的样子

<input type="text" name="phase1" ng-model="phase1" my-value="{{phase2 - phase3}}" my-model-value></input>

指令:

myApp.directive('myModelValue', function(){
return {
            restrict: 'A',
            require: 'ngModel',
            scope: {
                model: '=ngModel',
                value: '@myValue'
            },
            link: function (scope, element, attr, controller) {
                scope.model = scope.value;
            }
        };

});

该指令仅在加载时评估,但我想随着相关字段(阶段 2 和阶段 3)的变化不断更新/观察。

我可以从控制器更新值,但我想从 html 进行。请帮助我,它可能或反对角度的工作

【问题讨论】:

    标签: angularjs angularjs-directive angular-ngmodel angularjs-ng-model


    【解决方案1】:

    谢谢大家我知道我想做什么。这是我最后一个简单但有用的指令:)

    app.directive('myModelValue', function () {
            return {
                restrict: 'A',
                require: 'ngModel',
                scope: {
                    model: '=ngModel'
                },
                link: function (scope, element, attr, controller) {
                    attr.$observe('myModelValue', function (finalValue) {
                        scope.model = finalValue;
                    });
                }
            };
        });
    

    用法:

    <input type="text" ng-model="phase1" my-model-value="{{phase2 - phase3}}"></input>
    <input type="text" ng-model="phase1.name" my-model-value="{{valid angular expression}}"></input>
    

    【讨论】:

      【解决方案2】:

      为了持续观察 phase2/3 的变化,你可以使用 $scope.$watch 函数。

      这样的东西对你有用:

      link: function (scope, element, attr, controller) {
               scope.$watchCollection('[phase1,phase2]', function() {
                  //whatever you want to do over here            }
      

      并且在范围内也传递 phase1 和 phase2 值

      【讨论】:

        【解决方案3】:
        This will watch the  value expression and update the same when value will change
        
        myApp.directive('myModelValue', function(){
        return {
                    restrict: 'A',
                    require: 'ngModel',
                    scope: {
                        model: '=ngModel',
                        value: '@myValue'
                    },
                    link: function (scope, element, attr, controller) {
                        scope.$watch('value',function(newValue){
                              console.log(newValue);
                       });
                    }
                };
        });
        here value is a local scope so it will watch the expression 
        

        【讨论】:

        • 您可以将手表表达式简化为一个:$watch('phase2 + phase3', function()...)
        • 哦,对不起,我希望表达式成为我的任何自定义表达式。我不想为每个表达式编写另一个指令。想要编写一个接受表达式并对其进行评估的通用指令
        猜你喜欢
        • 2017-04-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        相关资源
        最近更新 更多