AngularJS: How to load JSON data onto a scope variable

后端 未结 5 1455
南笙
南笙 2021-02-07 21:46

I\'m creating a personal website where I can keep updating content without having to manipulate the HTML. I\'m trying to achieve this by simply loading and updating

5条回答
  •  深忆病人
    2021-02-07 22:07

    Change your mainController.js and JSON files as follows. Here i'm using wamp server as my local server.

    var myapp = angular.module('mainApp', []);
    myapp.controller('mainController', function($scope, $http) {
      $http.get('http://localhost/stackoverflow/angular/test1/database.json').success (function(data) {
        $scope.contents = data.records;
      })
    });
    

    JSON File:

    {
     "records":
     [
        {"heading":"MyHeading","description":"myDescription"}
     ]
    }
    

提交回复
热议问题