私は AngularJS の初心者ですが、それを習得することに非常に熱心であるため、私の無知を忘れてください。基本的なブログを作成するのに 2 つの問題があります。
問題 #1: $http を使用し、JSON を完全に受け取るコントローラーがあります。
これは app.js です:
angular.module('blogApp', []).config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/home', {
templateUrl : 'home.html'
}).when('/post/:postId', {
templateUrl : 'post.html',
controller : postCtrl
}).otherwise({
redirectTo : '/home'
});
}]);
これは controller.js です:
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*/
}
これは、#/post/123 が使用されたときに $scope.post が受け取る JSON です。
[
{
'id':'123',
'title':'title of the post',
'date':'2013-06-14',
'content':'This is the content of the post.<br>She had a yellow house.',
'meta_description':'description of my first post.'
}
]
問題は、controller.js で $scope.post.meta_description にアクセスできないことです。また、$scope.post[0].meta_description、data.post.meta_description、および data.post[0].meta_description を試してみましたが、肯定的な結果は得られませんでした。
問題 #2: post.html には、このコードがあります
<div>
<h1>{{post[0].title}}</h1>
<h2>Published on {{post[0].date}}</h2>
{{post[0].content}}
</div>
私は完全な AngularJS のチュートリアルを練習しました。{{post.title}} を使用する必要がありますが、AngularJS は何も出力しません。それを解決するために、{{post[0].title}} を使用します。なぜこれが起こるのですか?