AngularJS check-Objekt/array ist leer

Ich habe gelesen, viele Fragen und Antworten, niemand hat mir geholfen. Ich habe diese Funktion:

var model = {};

var mediaReproductionApp = angular.module("mediaReproductionApp",["ui.event",'ngAnimate']);

mediaReproductionApp.run(function ($http) {
    $http.get("movimenti_per_totem.json").success(function (data) {
        model.items = data;
    });
});

mediaReproductionApp.controller("MediaReproductionCtrl", function($scope, $http, $timeout) {
    $scope.item = model;

    $scope.playVideo = function(media) {
        return media ? "../gallery/video/" + media : null;
    }

    $scope.reproductionCodeIsEmpty = function() {
        return Object.keys($scope.item).length == 0;
    }

    $scope.endVideo = function() {
        $timeout(function() {
            $http.get("php/delete_record.php").success(function () {
                $http.get("movimenti_per_totem.json").success(function (data) {
                    $scope.item.items = data;
                });
            });
            if($scope.reproductionCodeIsEmpty()) {
                prelevaDati('../json/52.json', 'spot_creator', 'sc1', modello_SC, {});
                $scope.checkMediaData();
            }
        },1800);
    }

    $scope.checkMediaData = function() {
        $http.get("movimenti_per_totem.json").success(function (data) {
            $scope.item.items = data;
        });
        if($scope.reproductionCodeIsEmpty()) {
            $timeout(function(){$scope.checkMediaData();},2000);
        }
    }

    $scope.checkMediaData();
});

Dies ist meine JSON-Datei, wenn es nicht leer ist:

[ {"media":"zafferano_VP8.webm"}, {"media":"amaretti_VP8.webm"}, {"media":"passata_VP8.webm"}]

Es nie true zurück, wenn es leer ist. Ich habe auch versucht:

$scope.reproductionCodeIsEmpty = function() {
    return $scope.item.length == 0;
}

$scope.reproductionCodeIsEmpty = function() {
    return $scope.item == {};
}

$scope.reproductionCodeIsEmpty = function() {
    return angular.isUndefined($scope.item) || $scope.item === null;
}

$scope.reproductionCodeIsEmpty = function() {
    return angular.isUndefined($scope.item.items) || $scope.item.items === null;
}

Nichts funktioniert... u kann mir sagen, warum?
Danke!

  • Wir wissen nicht, was model ist oder wo du diesen code verwenden. Bieten eine minimale reproduzierbare Beispiel. Beachten Sie, dass return $scope.item == {}; würde niemals funktionieren wegen der unterschiedlichen Objekt-Referenzen
  • I ' V bearbeitet meine Frage! Mit mehr infos
  • Hinzugefügt, um meine Antwort.
  • console.log(data) und schauen, was drin ist, wenn die json leer ist
InformationsquelleAutor ProtoTyPus | 2016-09-17
Schreibe einen Kommentar