Display json array elements using AngularJS
As a newbie to angularjs, I am trying to use a controller to fetch JSON elements and display, but the desired result is not showing.
index.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js"></script>
<script src="../public/javascripts/processJson.js"></script>
</head>
<body>
<div ng-app="getJson"><div ng-controller="controller1">
<div ng-repeat="post in posts">
<div ng-switch-when="text"><input type="text" id="{{post.id}}" ng-model="post.value" placeholder="{{post.placeholder}}">
</div>
</div>
</div>
</div>
</body>
</html>
Controller: processJson.js
var getJson = angular.module('getJson', []).controller('controller1', function ($scope, $http) {
var url = "../../routes/fields.js";
console.log(url);
$http.get(url).success(function (data) {
$scope.posts = data;
});
});
fields.js
var express=require('express');
var router =express.Router();
router.get('/fieldlist',function(req,res){
var db=req.db;
var collection=db.get('domlist');
collection.find({},{},function(e,docs){
res.json(docs);
});
});
module.exports=router;
Also using mongodb to store and retrieve JSON data. The url http: // localhost: 3000 / fields / fieldlist returns my json array stored in mongodb.
source to share
Can you try $scope.posts = data.data
.
The HTTP request returns an object with a data property inside:
The response object has the following properties:
data - {string | Object} - The response body, transformed using the transform. status - {number} - HTTP response status code. headers - {function ([headerName])} - Function for getting the header. config - {Object} - the configuration object that was used to generate the request. statusText - {string} - HTTP response text.
For more information: API Reference - $ http
source to share
You have ng-switch-when, if there is no ng switch around, it gives me an error:
Error: [$compile:ctreq] Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found!
I prepared JSFiddle https://jsfiddle.net/5ab19ozh/
source to share