Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS - How to access directly a $scope's row/value from the controller
    text
    copied!<p>I'm newbie in AngularJS but am very eager to master it, so please forget my ignorance. I have two problems making a basic blog:</p> <p><strong>Problem #1:</strong> I have a controller that uses $http and receives a JSON perfectly.</p> <p>This is the app.js:</p> <pre><code>angular.module('blogApp', []).config(['$routeProvider', function($routeProvider) { $routeProvider.when('/home', { templateUrl : 'home.html' }).when('/post/:postId', { templateUrl : 'post.html', controller : postCtrl }).otherwise({ redirectTo : '/home' }); }]); </code></pre> <p>This is the controller.js:</p> <pre><code>function postCtrl($scope, $http, $routeParams) { $http.get('post.php?id=' + $routeParams.postId).success(function(data) { $scope.post = data; }); $scope.description = $scope.post.meta_description; /*I can't access this value*/ </code></pre> <p>}</p> <p>And this is the JSON that $scope.post receives when #/post/123 is used:</p> <pre><code>[ { 'id':'123', 'title':'title of the post', 'date':'2013-06-14', 'content':'This is the content of the post.&lt;br&gt;She had a yellow house.', 'meta_description':'description of my first post.' } ] </code></pre> <p>The problem is that in the controller.js, I can't access $scope.post.meta_description. Also I have tried with $scope.post[0].meta_description, with data.post.meta_description and with data.post[0].meta_description without a positive result.</p> <hr> <p><strong>Problem #2:</strong> In post.html, I have this code</p> <pre><code>&lt;div&gt; &lt;h1&gt;{{post[0].title}}&lt;/h1&gt; &lt;h2&gt;Published on {{post[0].date}}&lt;/h2&gt; {{post[0].content}} &lt;/div&gt; </code></pre> <p>I practiced the full AngularJS' tutorial, and should be using {{post.title}} but AngularJS doesn't print anything. To solve it, I use {{post[0].title}}. Why happens this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload