javascriptjqueryangularng-init

Add multi dimension object in ng-init in angular js


I want to add multi dimension object in ng-init like

object({
    friends:[
        {name:John,phone:555-1276},
        {name:Mary,phone:800-BIG-MARY},
        {name:Mike,phone:555-4321},
        {name:Adam,phone:555-5678},
        {name:Julie,phone:555-8765},
        {name:Juliette,phone:555-5678}
   ],
   relative:[
       {name:Afaq,phone:555-1276},
       {name:Mary,phone:800-BIG-MARY},
       {name:Mike,phone:555-4321},
       {name:Adam,phone:555-5678},
       {name:Julie,phone:555-8765},
       {name:Juliette,phone:555-5678}
    ]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

but when i add same like this then show me this error

"Error: [$parse:syntax] Syntax Error: Token 'Carrara' is unexpected, expecting [}] at column 1754 of the expression [object("

so what to do....

Thanks in advance...


Solution

  • Your object structure is wrong. Try this

     var x = {
     	"parent": {
     		"friends": [{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			}
     		],
     		"relative": [{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			},
     			{
     				"name": "John",
     				"phone": "555 - 1276"
     			}
     		]
     	}
     }
     console.log(x);

    You can always use Json lint to validate your json