1

I am new to angular.js just started learning, i want to Display the array defined in the Controller, But When I am trying to Display Its Showing empty page. I know if i change the ng-repeat=post in post1 to ng-repeat=post in posts" it ll work. But i want show the Error either in the Console or in the Browser. Please Can anybody help me.

var mainapp = angular.module('mainApp', []);
mainapp.controller('control', function ($scope) {

    $scope.posts = [
        {title: 'post 1', upvotes: 5},
        {title: 'post 2', upvotes: 2},
        {title: 'post 3', upvotes: 15},
        {title: 'post 4', upvotes: 9},
        {title: 'post 5', upvotes: 4}
    ];
   
});
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
</head>
<body ng-app="mainApp" ng-controller="control">

<div ng-repeat="post in post1 | orderBy:'upvotes'">
    <p>{{post.title}}-upvotes {{post.upvotes}}</p>
</div>

</body>
</html>

1
  • It will never give you error. cz $scope is an object and in view all are scope property. If property doesn't exists in $scope , it'll be added. It won't throw error. Commented Jan 13, 2016 at 11:24

2 Answers 2

1

This is not an error at all from angular point of vew. In fact it is quite usual to use variables in template which are undefined for now, but will get some value later.

I.e. imagine that you have in controller:

$timeout(function() {
    $scope.post1 = [{title : 'test'}];
}, 100)
Sign up to request clarification or add additional context in comments.

Comments

1

Use ng-if directive to check "post1" is undefined and display error message.

<div ng-repeat="post in post1 | orderBy:'upvotes'">
    <p>{{post.title}}-upvotes {{post.upvotes}}</p>
</div>

<div ng-if="post1==undefined">
     Error Message
</div>

Comments

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.