1

Object defined in scope:

$scope.products = [ { name: 'custom', category: { name:'custom', templateAttribute: [ {attribute: 'material'}, {attribute: 'soles'}, {attribute: 'size'} ] } } ]; 

HTML:

<table class="table" ng-repeat="attr in products.category.templateAttribute"> <tbody> <tr> <td> <input value="{{attr.attribute}}" /> </td> <td> <input placeholder="name" ng-model="product.attributes[attr.attribute].name" /> </td> <td rowspan="2"> <button type="button" ng-click="addItem(product.category.templateAttribute, attr)"> add </button> </td> </tr> </tbody> </table> 

What I want output to look every attribute has input form

How it seems like it should work:

<table class="table" ng-repeat="attr in products.category.templateAttribute"> 

How to fix its?

9
  • 3
    That isn't JSON. It is JavaScript.
    – Quentin
    CommentedDec 28, 2015 at 10:46
  • 3
    @KornelitoBenito — If it was JSON then it wouldn't start with $scope.products =. If it was JSON then the property names would be strings. If it was JSON then the strings would be delimited by double quote characters. If it was JSON then it wouldn't be trailed by a semi-colon.
    – Quentin
    CommentedDec 28, 2015 at 10:47
  • 4
    @KornelitoBenito: No, it isn't. It's a JavaScript object initializer. JSON is a textual notation used for data interchange which is based on JavaScript initializer notation, but is a subset of it. If you're writing source code, and not dealing with a string, you're not dealing with JSON.CommentedDec 28, 2015 at 10:47
  • 1
    Today I learned. Thanks :)CommentedDec 28, 2015 at 10:48
  • 1
    What is your problem/question? This code should work fine.
    – idmean
    CommentedDec 28, 2015 at 10:51

2 Answers 2

2

As $scope.product is an array $scope.product.category itself is undefined

it must be like

<table class="table" ng-repeat="attr in products[0].category.templateAttribute"> 

if products is dynamic

<table class="table" ng-repeat="product in products"> <tr> <td ng-repeat="attribute in product.category.templateAttribute"> <td><input value="{{attr.attribute}}" /></td> <td> <input placeholder="name" ng-model="product.attributes[attr.attribute].name" /> </td> <td rowspan="2"> <button type="button" ng-click="addItem(product.category.templateAttribute, attr)"> add </button> </td> </td> </tr> </table> 

so table will be repeating according to the objects in $scope.products array

    0

    If provided HTML code is your whole template, there is an error in assuming that ng-repeat="attr in products.category.templateAttribute" iterates over "templateAttribute" array in products[0].category object.

    You actually need two ng-repeats, one to iterate over products array, and another to iterate over category.templateAttribute in each product:

    <table class="table" ng-repeat="product in products"> <tr> <td ng-repeat="attr in product.category.templateAttribute"> <input value="{{attr.attribute}}" /> ..... </td> </tr> </table>

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.