0

I've just started learning Angular JS but soon I got a problem which I don't know how to resolve.

This code prints:

<h1>{{author[0].name}}</h1>
<p>{{author[0].title+ ', '+author[0].company}}

instead of:

<h1>MKJ</h1>
<p>Web Developer, Student Organization</p>

The code is given here as well:

<!doctype html>
<!-- Declaring the ng-app -->
<html ng-app="myApp">
<head>
    <title>Parking</title>
    <!-- Importing the angular.js script -->
    <script src="lib/angular/angular.min.js"></script>
    <script>
        var myApp = angular.module("myApp", []);
        myApp.controller("MyController", function ($scope){
            $scope.author = {
                'author': 'MKJ',
                'title': 'Web Developer',
                'company': 'Student Organization',
            };
        }
    </script>
</head>
<!-- Attaching the view to the MyController -->
<body ng-controller="MyController">
<h1>{{author[0].name}}</h1>
<p>{{author[0].title+ ', '+author[0].company}}

See this snippet or correct the code on JS fiddle?

var myApp = angular.module("myApp", []);
    myApp.controller("MyController", function ($scope){
        $scope.author = {
            'author': 'Ravy VIllalbobs',
            'title': 'Staff Author',
            'company': 'Lynda.com',
        };
}
<div ng-controller="MyController">
    <h1>{{author.name}}</h1>
    <p>{{author.title+ ', '+author.company}}
</div>

1 Answer 1

1

$scope.author is an Object so you don't need to specify index in brackets here

<h1>{{author.name}}</h1>
<p>{{author.title+ ', '+author.company}}

updated fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

not working when I removed the square brackets as you told. Can you plz fix it on fiddlejs?
@M.KhuramJaved your fiddle has a few syntax issues and also you forgot to include angular lib, here is updated one: jsfiddle.net/xL1sn5e5/6
Yes! Thanks a lot Artem Petrosian! It's working now on my pc as well as fiddle!
you can also remove string concatenation in second block, because it's not necessary {{author.title}}, {{author.company}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.