AngularJS在加载内容后开始观看

前端之家收集整理的这篇文章主要介绍了AngularJS在加载内容后开始观看前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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);
}

主要目标是按所选复选框计算成本.

但是在加载页面上我看到警报,如何在加载内容后才开始观看?

第二个问题:
在div上进行ng-repeat.所有重复的div都像

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

这是正常的吗?

解决方法

尝试添加标志,或在jsfiddle link上尝试.

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,{
            id: 3,{
            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);
        }
    });
}

猜你在找的Angularjs相关文章