Why my form doesn't work when it's inside of ng-repeat?

mattsky

I'm learning AngularJS and I making simple task management application. Application is connecting to external rest service. I try to render all my tasks in activities (task's container) with add task button but when form is in loop then it's not sending anything.

Here's my html code:

<div id="task-group" ng-repeat="activity in activities | filter:query">
        <p>
        <h2>{{activity.activityName}}</h2></p>
        {{activity.activityId}}
        <form name="form" ng-submit="addTask()">
        <input type="text" ng-model="sendTaskName">
        <input type="hidden" name="{{activity.activityId}}" >
            <input type="submit" class="btn-primary" value="Add task">
        </form>
        <ul class="task-list">
            <li ng-repeat="task in activity.tasks">
                <p>{{task.taskName}}</p>
            </li>
        </ul>
    </div>

Add task function in my controller:

$scope.addTask = function(){
      $http({
          method: 'POST',
          url: 'http://localhost:8080/add-task',
          data: {taskName: $scope.sendTaskName, activityId: $scope.sendActivityId}
      }).success(function(){
          getActivities()
          $scope.sendTaskName = ''
      })
  }

EDIT: @Mickael helps me to solve my problem. The solution is in his answer. Thank you very much once again ! ;)

Mickael

You are using ng-repeat and inside each iteration, you bind your inputs with the ng-model directive.

What happens here is that on each iteration, a new scope is created. When you start typing inside your input, the sendTaskName variable is created inside the scope of the iteration, not the scope of your controller.

When your form is submitted, you submit the variable of the scope of your controller, that's why you do not send anything.

To solve your problem, I suggest that you give parameters to your addTask method:

 <div id="task-group" ng-repeat="activity in activities | filter:query">
    <p>
    <h2>{{activity.activityName}}</h2></p>
    {{activity.activityId}}
    <form name="form" ng-submit="addTask(activity.activityId, sendTaskName); sendTaskName = '';">
        <input type="text" ng-model="sendTaskName">
        <input type="submit" class="btn-primary" value="Add task">
    </form>
    <ul class="task-list">
        <li ng-repeat="task in activity.tasks">
            <p>{{task.taskName}}</p>
        </li>
    </ul>
</div>

And use these parameters in your controller:

$scope.addTask = function(activityId, taskName) {
  $http({
      method: 'POST',
      url: 'http://localhost:8080/add-task',
      data: {taskName: taskName, activityId: activityId}
  }).success(function(){
      getActivities();
  })
}

Next time, with a plunkr, I will be able to fix your code ;)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

From Dev

h:form inside ui:repeat doesn't work as expected

From Dev

ng-repeat doesn't work with data in my case

From Dev

Angular: Why doesn't CSS justification work with ng-repeat?

From Dev

Using ng-repeat on a JSON object doesn't work - why?

From Java

Adding parameter to ng-click function inside ng-repeat doesn't seem to work

From Dev

ng-click inside ng-repeat doesn't work with :focus and display

From Dev

ng-click inside ng-repeat doesn't work with :focus and display

From Dev

Why my ng-show doesn't work?

From Dev

Vertical Alignment Doesn't Work when Form is Added inside Div

From Dev

nested ng-repeat doesn't work

From Dev

AngularJS ng-repeat doesn't work

From Dev

nested ng-repeat doesn't work

From Dev

AngularJS ng-repeat doesn't work

From Dev

ng-repeat doesn't work properly

From Dev

Why do ES6's Proxy doesn't work when used inside a method of class?

From Dev

d3-driven directive transition doesn't work inside ng-repeat

From Dev

Why doesn't my camera work when there's a kernel module for the driver?

From Dev

Why doesn't ulimit -n work when called inside a script?

From Dev

Why doesn't ulimit -n work when called inside a script?

From Dev

Why doesn't $1 work when used inside $'...'?

From Dev

Why doesn't my IF work

From Dev

Any reason why ng-repeat-start doesn't work despite using the latest angularJS version?

From Dev

Why doesn't my Gmail user/password work when deploying ASP.NET to Compute Engine inside Visual Studio?

From Dev

ng-repeat and ng-scrollbar doesn't work together

From Dev

ng-repeat and ng-scrollbar doesn't work together

From Dev

Why doesn't my operation work when I use BigDecimal?

From Dev

Why doesn't my command work when aliased?

Related Related

  1. 1

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  2. 2

    My AngularJS view is able to access it's controller's scope, but ng-repeat doesn't work

  3. 3

    h:form inside ui:repeat doesn't work as expected

  4. 4

    ng-repeat doesn't work with data in my case

  5. 5

    Angular: Why doesn't CSS justification work with ng-repeat?

  6. 6

    Using ng-repeat on a JSON object doesn't work - why?

  7. 7

    Adding parameter to ng-click function inside ng-repeat doesn't seem to work

  8. 8

    ng-click inside ng-repeat doesn't work with :focus and display

  9. 9

    ng-click inside ng-repeat doesn't work with :focus and display

  10. 10

    Why my ng-show doesn't work?

  11. 11

    Vertical Alignment Doesn't Work when Form is Added inside Div

  12. 12

    nested ng-repeat doesn't work

  13. 13

    AngularJS ng-repeat doesn't work

  14. 14

    nested ng-repeat doesn't work

  15. 15

    AngularJS ng-repeat doesn't work

  16. 16

    ng-repeat doesn't work properly

  17. 17

    Why do ES6's Proxy doesn't work when used inside a method of class?

  18. 18

    d3-driven directive transition doesn't work inside ng-repeat

  19. 19

    Why doesn't my camera work when there's a kernel module for the driver?

  20. 20

    Why doesn't ulimit -n work when called inside a script?

  21. 21

    Why doesn't ulimit -n work when called inside a script?

  22. 22

    Why doesn't $1 work when used inside $'...'?

  23. 23

    Why doesn't my IF work

  24. 24

    Any reason why ng-repeat-start doesn't work despite using the latest angularJS version?

  25. 25

    Why doesn't my Gmail user/password work when deploying ASP.NET to Compute Engine inside Visual Studio?

  26. 26

    ng-repeat and ng-scrollbar doesn't work together

  27. 27

    ng-repeat and ng-scrollbar doesn't work together

  28. 28

    Why doesn't my operation work when I use BigDecimal?

  29. 29

    Why doesn't my command work when aliased?

HotTag

Archive