Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS$http.get返回未定义的_Javascript_Json_Angularjs - Fatal编程技术网

Javascript AngularJS$http.get返回未定义的

Javascript AngularJS$http.get返回未定义的,javascript,json,angularjs,Javascript,Json,Angularjs,我正在构建一个小的角度应用程序。我从这里开始:。我只更改了以下文件: /app/view1/view1.js "严格使用", angular.module('myApp.view1', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/view1', { templateUrl: 'view1/view1.html', controller: 'V

我正在构建一个小的角度应用程序。我从这里开始:。我只更改了以下文件:

  • /app/view1/view1.js "严格使用",

    angular.module('myApp.view1', ['ngRoute'])
    
    .config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/view1', {
        templateUrl: 'view1/view1.html',
        controller: 'View1Ctrl'
      });
    }])
    
    
    .controller('View1Ctrl', ['$scope', '$http',
      function ($scope, $http) {
        $http.get('/app/data/events.json').success(function(data) {
          $scope.events = data.record;
        }).error(function(data, status){
            alert("error "+data+' | '+status);
        });
    
        $scope.orderProp = 'date';
      }]);
    
  • /app/view1/view1.html

    p>Available Events</p>
    
    <div ng-controller="EventListController">
      <ul>
        <li ng-repeat="event in events">
            <p>{{event.name}}</p>
            <div>{{event.location}}</div>
            <div>{{event.description}}</div>
            <div>{{event.date}}</div>
    
        </li>
    
      </ul>
    
     <p>Total number of events: {{events.size}}</p>
    </div>
    
    p>可用事件

    • {{event.name}

      {{event.location} {{event.description} {{event.date}
    事件总数:{{events.size}

  • 显示页面时,我收到以下消息:

    “错误未定义|未定义”

    我已经检查了几次代码,没有找到为什么json文件没有加载。如果我尝试在浏览器中访问json数据,则会加载json数据


    问题是:为什么不加载json文件?

    您应该使用如下$http方法:

    // Simple GET request example:
    $http.get('url').then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    
    代码
    数据
    状态
    中的变量未定义

    您应该像这样更新代码:

     $http.get('/app/data/events.json')
     .then(function successCallback(response) {
          $scope.events = data.record;
      },
      function errorCallback(response) {
          alert(response);
      });
    

    这将解决您的问题。

    打开控制台(F12)并检查,我认为问题在于您的“本地主机”。设置一些域名,然后再试一次。在Mac上使用Option+Command+J尝试从url中删除该应用程序,并按如下方式键入/data/events.json