1

我想知道如何在$watch表达式中触发第二个摘要循环。似乎在一个$watch表达式中只有一个摘要循环是可能的。我试图打电话$apply,但它崩溃了消息“正在消化”。

function TestCtrl($scope) {
    var purchase_orders = [
        {id : 1, name : 'Purchase Order One', multiple_day_rates : true },
        {id : 2, name : 'Purchase Order Two', multiple_day_rates : false },
        {id : 3, name : 'Purchase Order Three', multiple_day_rates : true } ];

$scope.purchase_orders = purchase_orders;

$scope.$watch(
    "poId",
    function( poId ) {
        console.log("watch triggered for po id");
        $scope.multiple_day_rates = null; //seems to have no effect..
        //$scope.$apply(function () {
        //          $scope.multiple_day_rates = null;
        //        }); --> throws digest already in progress
        var purchase_order_obj = jQuery.grep($scope.purchase_orders, function(e){ return e.id == $scope.poId; });
        if(typeof purchase_order_obj[0] != 'undefined'){
            if(purchase_order_obj[0].multiple_day_rates == true){
                console.log("before setting multiple day rates to true");
                $scope.multiple_day_rates = true;
            }
            else{
                console.log("before setting multiple day rates to false");
                $scope.multiple_day_rates = false;
                $scope.day_rate_check=true;
            }
        }
      }
    ); 

$scope.$watch(
    "multiple_day_rates",
    function( multiple_day_rates ) {
        console.log("watch triggered for multiple day rates");
        }
    ); 
}

我的目标是,当$scope.multiple_day_rates设置好之后,我想运行一些代码。我还想检测它何时设置为trueto true。由于 AngularJS 正在进行某种脏检查(仅在值更改时触发监视),我必须在$scope.multiple_day_rates之前设置为 null,以便检测到更改。但不知何故,在一个表达式中似乎$watch只有一个消化循环是可能的。这是JSFiddle。您可以看到何时从“采购订单一”更改为“采购订单”两次都没有发生任何multiple_day_rates事情true

4

1 回答 1

1

这是你想要达到的目标吗?

function TestCtrl($scope, $timeout) {
    var purchase_orders = [{
        id: 1,
        name: 'Purchase Order One',
        multiple_day_rates: true
    }, {
        id: 2,
        name: 'Purchase Order Two',
        multiple_day_rates: false
    }, {
        id: 3,
        name: 'Purchase Order Three',
        multiple_day_rates: true
    }];
    $scope.purchase_orders = purchase_orders;

    $scope.$watch(
        "poId",
        function (poId) {
            console.log("watch triggered for po id");
            $scope.multiple_day_rates = null; //seems to have no effect..
            //$scope.$apply(function () {
            //          $scope.multiple_day_rates = null;
            //        }); --> throws digest already in progress
            $timeout(function () {
                var purchase_order_obj = jQuery.grep($scope.purchase_orders, function (e) {
                    return e.id == $scope.poId;
                });
                if (typeof purchase_order_obj[0] != 'undefined') {
                    if (purchase_order_obj[0].multiple_day_rates == true) {
                        console.log("before setting multiple day rates to true");
                        $scope.multiple_day_rates = true;
                    } else {
                        console.log("before setting multiple day rates to false");
                        $scope.multiple_day_rates = false;
                        $scope.day_rate_check = true;
                    }
                }
            });
        }
    );

    $scope.$watch(
        "multiple_day_rates",
        function (multiple_day_rates) {
            console.log("watch triggered for multiple day rates");
        }
    );
}

http://jsfiddle.net/qhnr5/

于 2013-06-27T15:08:29.970 回答