5

AngularJS の新機能です。最初のアプリを作成しようとして、問題が発生しました。チェックボックスのリストを持っている

<div class='order-box row-fluid' ng-repeat="order in orders" ng-model="orders">
   <input type="checkbox" value="{{ order.id }}" ng-model="order.selected">
<div>

注文は、読み込みページの ajax クエリによってサーバーから入力されます。

function OrdersCtrl($scope, Order){
    $scope.orders = Order.query();
    $scope.$watch('orders', function(v){
        if (!v) return;
        alert(v);
    }, true);
}

主なターゲットは、チェックボックスを選択してコストを計算することです。

しかし、ロードページにアラートが表示されます。コンテンツをロードした後にのみウォッチを開始するにはどうすればよいですか?

2 番目の小さな質問: div で ng-repeat を使用します。そして、すべての繰り返しdivのような

<div class="order-box row-fluid ng-scope ng-pristine ng-valid" ng-repeat="order in orders" ng-model="orders">

それは正常ですか?

4

2 に答える 2

4

フラグを追加するか、jsfiddleリンクで試してください。

function OrdersCtrl($scope, $timeout) {
    $scope.orders = [];
    var first = true; //this doesn't belong to any scope, it will be deallocated after the current cycle.

    var ajax = function () {
        $scope.orders = [{
            id: 1,
            selected: true
        }, {
            id: 2,
            selected: true
        }, {
            id: 3,
            selected: true
        }, {
            id: 4,
            selected: true
        }];
    };

    $scope.$watch('orders', function (n, o) {
        if (first) {
            $timeout(ajax, 2000); //simulate the ajax call. Assume data comes back in 2 seconds.
        } else {
            alert(n);
        }
    });
}
于 2013-07-25T00:34:10.410 に答える