How to pass bound values from view to controller using ng-init

Asked 2 years ago, Updated 2 years ago, 99 views

As you can see in the title, I have a question because I am struggling with how to pass bound values from view to controller using ng-init.

The assumption is that there are n items_list in one shop, and there are multiple items that are tied together.

shop(1)->item_list(n)->item(n)

In this situation, I'm trying to add a new item tied to the item_list from the new_item add form, but I'm worried about how to pass the id of the item_list I'm trying to add to the controller...

If you try to run it as it is, {{item_list.id}} does not contain the value yet (I'm not sure if it's correct) at ng-init, so it will be passed as undefined.

Is ng-init impossible to pass the bounded value to the controller?Also, if there is any other best way, I would like to take this opportunity to let me know.Thank you for your cooperation.

ul
  link-repeat = "item_list in shop.item_lists"
    span item_list.id->{{item_list.id}}
    ul
      link-repeat = "item in item_list.items"
        span item.id->{{item.id}}
        input type="submit" value="delete"ng-click="deleteItem(item)"

      form id="new_item"ng-submit="addItem(itemBody,itemListId)"
        input type="text" id="itemBody"ng-model="itemBody"ng-init="itemListId={{item_list.id}}"
        input type="submit" value="Add ({{item_list.id}}}"

-

angular.module('angularApp').controller "ShopController", ($scope)->
  $scope.addItem=(itemBody,itemListId)->
    alert(itemBody)
    alert(itemListId)

javascript angularjs

2022-09-30 20:18

1 Answers

If the shop value is not included asynchronously,

ng-init="itemListId=item_list.id"

I think it will be worth it.

Before that, I think it would be better to erase ng-init and include addItem(item_list.body,item_list.id) and bind value directly.


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.