3

Being a novice to angularjs framework I'm attempting to use a controller to retrieve a JSON and display elements,but the desired result is not displayed.

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 utilising mongodb to store and retrieve as JSON data.. URL http://localhost:3000/fields/fieldlist returns my json array stored in mongodb.

4
  • Can you show us an example of what the JSON looks like please? Commented Aug 7, 2015 at 9:04
  • Please include where you facing problem? Do you get desired data in $scope.posts? Commented Aug 7, 2015 at 9:04
  • @PhilKearney added more code Commented Aug 7, 2015 at 9:58
  • @Navaneeth added more code Commented Aug 7, 2015 at 9:58

2 Answers 2

2

Can you try $scope.posts = data.data.

The HTTP request will return an object with the data property inside:

The response object has these properties:

data – {string|Object} – The response body transformed with the transform functions. status – {number} – HTTP status code of the response. headers – {function([headerName])} – Header getter function. config – {Object} – The configuration object that was used to generate the request. statusText – {string} – HTTP status text of the response.

For more information: API Reference - $http

Sign up to request clarification or add additional context in comments.

Comments

1

You have a ng-switch-when without an ng-switch around that causes me an error:

Error: [$compile:ctreq] Controller 'ngSwitch', required by directive 'ngSwitchWhen', can't be found!

I've prepared a JSFiddle https://jsfiddle.net/5ab19ozh/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.