更新更新模型与延迟

问题描述 投票:0回答:1

大家。我在使用angularjs时遇到了麻烦。我为input [type =“text”]创建了自定义指令,并作为模型传递给变量。但ng-change事件称为函数,具有先前的变量值。示例:状态:0,类型1,函数 - 0.状态:1,类型548,函数 - 1.状态:548,类型3,函数548。

我的HTML:

<div ng-controller="simpleCTRL">

<mr-textfield is-required="true" value="val" title="Minutes" type="text" change="ChangeVal()"></mr-textfield>
<input type="text" ng-model="val" ng-change="ChangeVal()"/>

</div>

</div>

和js:

<!-- language: lang-js -->    
angular.module('SimpleInterestApp', [])
  .controller('simpleCTRL', function($scope) {

  $scope.val = 0;

    $scope.ChangeVal = function() {
      console.log($scope.val);
    };


  })
  .directive('mrTextfield', function () {
    return {
        restrict: 'E',
        template: "<div class='textfield'><input type='{{::type}}' ng-required='isRequired' ng-model='value' ng-change='change()'><span class='bar'></span><label>{{::title}}</label></div>",
        replace: true,
        transclude: true,
        scope: {
            value:"=",
            title:"@",
            type:"@",
            isRequired:"=",
            change:"&"
        }
    };
});
javascript angularjs angularjs-directive two-way-binding
1个回答
0
投票

console.log中包裹$timeout

$scope.ChangeVal = function() {
  $timeout(function() {
    console.log($scope.val);
  })
};

working plunker

© www.soinside.com 2019 - 2024. All rights reserved.