我对Angularjs很陌生,所以提前道歉是一个基本的解决方案。我希望能够使用angularjs访问并添加到外部.json文件。当我在工厂内硬编码数据时,它显示良好。但是,当我尝试使用angular的$http访问数据时,没有显示任何内容。
这是我的工厂:

sampleApp.factory('findSamplesFactory', function($http) {
    var samples = $http.get('sample.json').success(function(response) {
        return response;
    });

    var factory = {};
    factory.getSamples = function() {
        return samples;
    };
    factory.insertSamples = function(sampleNumber, sampleBox, sampleRow, sampleLevel) {
        samples.push({
            number: sampleNumber,
            box: sampleBox,
            row: sampleRow,
            level: sampleLevel
        });
    };

    return factory;
});

这是我设置的控制器
sampleApp.controller('sampleAppController', function ($scope, findSamplesFactory) {
    $scope.samples = findSamplesFactory.getSamples();

    $scope.addSamples = function() {
        var sampleNumber = $scope.newSample.number;
        var sampleBox = $scope.newSample.box;
        var sampleRow = $scope.newSample.row;
        var sampleLevel = $scope.newSample.level
        findSamplesFactory.insertSamples(sampleNumber, sampleBox, sampleRow, sampleLevel);
        $scope.newSample.number = ' ';
        $scope.newSample.box = ' ';
        $scope.newSample.row = ' ';
        $scope.newSample.level = ' ';
    };
});

json文件验证正常。任何帮助都将不胜感激!
编辑:这是我的完整应用程序的一个漏洞:http://plnkr.co/edit/2itJiB?p=preview

最佳答案

我改变了下面的内容,效果很好。注意$scope.samples=data.data的变化;

sampleApp.controller('sampleAppController', function ($scope, findSamplesFactory) {

    findSamplesFactory.getSamples().then(function(data){
      $scope.samples = data.data;  // I changed this.
    });
  ...Your code as it is...
  });

检查[Plunker]:http://plnkr.co/edit/sZTme6?p=preview

10-08 15:07