Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:14:48+00:00 2026-06-18T01:14:48+00:00

When using a ng-repeat, how should I use a controller inside it? For example,

  • 0

When using a ng-repeat, how should I use a controller inside it?

For example, if I’m looping over a set of days in a week:

<ul ng-controller="WeekCtrl">
    <li ng-repeat="d in days">
        <span ng-controller="DayCtrl">
            {{dayOfWeek}} {{date}}: {{info}}
        </span>
    </li>
</ul>

But my DayCtrl wants to know what day d it is, so has to pull that out of the scope:

app.controller('DayCtrl', function($scope){
  $scope.date = $scope.d.date;
  $scope.dayOfWeek = 
    ['Mon','Tue','Wed','Thr','Fri','Sat','Sun']
    [$scope.d.date.getDay()];
  $scope.info = '... extra info...';
});

But this creates a dependency between the display and the controller. Ideally, I’d like to pass in d.date as an argument.

I could write a directive+controller and pass d.date in as an attribute. But that means I have to write a lot more and move the day’s html into separate template, and I don’t intend to use the DayCtrl elsewhere.

Alternatively, I could try and use <span ng-init="date=d.date"> but again, this feels dirty.

What’s the right way of doing this.

Full example code on Plunker: http://plnkr.co/edit/wUxNFSEGjcDN7KlOLYdv which shows the problem I’m having with days and weeks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:14:50+00:00Added an answer on June 18, 2026 at 1:14 am

    To me, this seems like a job for a directive, with d.date specified as an attribute.
    I don’t know how info is populated, but potentially your directive would not need a controller.

    The day’s HTML doesn’t have to be in a separate/directive template, it can remain in the HTML:

     <li ng-repeat="d in days">
        <day date="d.date">
           {{dayOfWeek}} {{date}}: {{info}}
        </day>
     </li>
    

    Directive:

    app.directive('day', function() {
      return {
        restrict: 'E',
        scope: { date: '=' },
        link: function(scope) {
         scope.dayOfWeek =  ['Mon','Tue','Wed','Thr','Fri','Sat','Sun'][scope.date.getDay()];
         scope.info = '... extra info...';
        }
      };
    });
    

    Plunkr.

    FYI, if you want to use ng-init, you can include it with the ng-controller:

    <span ng-controller="DayCtrl" ng-init="date=d.date">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Allen Holub wrote “You should never use get/set functions”, is he correct?
I have a set of items that I want to filter in ng-repeat using
Using BreezeJS with AngularJS gives errors. For example when using 'filter' in an ng-repeat
I am using <ui:repeat> for displaying a list. Now I want to export whole
I am running a windows service and using a loop and Thread.Sleep to repeat
Is it possible to have transparent background image using CSS3? background: url(../img/bg_paperlines.jpg) repeat-x; Using
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Should I be using RegularExpressions to do this? Possible to structure the results as
I'm using JsRender to render a template client-side. However, I'm unable to use the
I have a use case that should be rather common but I can't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.