2

This should be simple but I'm not able to solve this and have no idea where I am going wrong. I have an angular module that is supposed to repeat the data that is a JSON array; My controller and module looks like the one below

 (function() { var timeslots = data; var app = angular.module('TimeSlot', []); app.controller("TimeSlotController", function(timeslots) { this.timeslots = JSON.parse(timeslots); }); })(); <div ng-app="TimeSlot"> <div class="col-md-12" ng-controller="TimeSlotController as slot" ng-repeat="item in slot.timeslots" > <div class="col-md-4 timeblock"> <h3 class="event-type-name">{{ item.time }} Hour Appointment</h3> <div class="description mts">{{ item.description}}</div> <div class="cost"><i class="fa fa-euro"></i>{{ item.cost }}</div> </div> </div> </div> </div> <!-- jQuery --> <script src="http://code.jquery.com/jquery.js"></script> <!-- Bootstrap JavaScript --> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var data = {{{ timeslot }}} </script> <script src="/js/timeslot.js"></script> 

This is the data that I am trying to parse,

vardata=[ { "time": 1, "description": "Here lies 1", "cost": "10" }, { "time": 2, "description": "Here lies 2", "cost": "20" }, { "time": 3, "description": "Here lies 3", "cost": "10" } 

]

Any help will be appreciated, I have taken a look at other posts and I have not been able to figure it out, so sorry if it is a duplicate post.

5
  • You're missing a key portion of code – where is slot.timeslots declared?
    – Shai
    CommentedNov 26, 2014 at 17:48
  • why are controllers ng-repeated?CommentedNov 26, 2014 at 17:48
  • 1
    In Angular you normally use $scope to hold your data and inject it in your Controllers and such.
    – PM 77-1
    CommentedNov 26, 2014 at 17:53
  • @PM77-1 , actually in 1.3 $scope is not required anymore and the this.timeslots is valid way of assigningCommentedNov 26, 2014 at 17:59
  • Thanks, I got this from the CodeSchool tutorial but now I have another problem, the fields inside {{}} are not displaying at all.CommentedNov 26, 2014 at 18:06

3 Answers 3

2

is data global? you can not to inject simple variables to your controller.
Try to delete argument from controller function.
Also why you parse already parsed data?

Just pust your data to $scope:

app.controller("TimeSlotController", function($scope) { $scope.timeslots = timeslots; }); 

And use it inside your controllers view:

<div ng-controller="TimeSlotController"> <div class="col-md-12" ng-repeat="item in timeslots"> {{item}} </div> </div> 
1
  • data is global,kind of new to the whole angular way of doing things still learning, I'll try it out.CommentedNov 26, 2014 at 17:50
0

If you repeat the controller, then the variable timeslots is not available on the same line.
Try your HTML like so:

<div class="col-md-12" ng-controller="TimeSlotController as slot" > <div class="col-md-4 timeblock" ng-repeat="item in slot.timeslots"> <h3 class="event-type-name">{{ item.time }} Hour Appointment</h3> <div class="description mts">{{ item.description}}</div> <div class="cost"><i class="fa fa-euro"></i>{{ item.cost }}</div> </div> </div> 
2
  • Thanks, my mistake was with the ng-repeat thing; thanks a lot you saved me a lot of timeCommentedNov 26, 2014 at 17:52
  • It is angular via backdoor using. Bad things happened if you are put your data without scope :)
    – vp_arth
    CommentedNov 26, 2014 at 17:55
0

You need to use $scope, with which angular bind data from controller to views and vicevers.

app.controller('TimeSlotController', function ($scope){ $scope.timeslots = JSON.parse(timeslots); }); 

So, in your view you just spell it like this:

 <div ng-repeat="item in timeslots"> 

ps: sqs my english

1
  • Where you define timeslots provider?
    – vp_arth
    CommentedNov 26, 2014 at 17:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.