2016-03-16 172 views
0

我廠Angularjs發表評論,

var obj = { 
    posts: [ 
    { 
     "text":"text", 
     "date":null, 
     "like":0, 
     "comments":[ 
      {"comment":"Yorum 1", "like":100}, 
      {"comment":"Yorum 2", "like":200}, 
      {"comment":"Yorum 3", "like":300} 
     ] 
    }, 
    { 
     "text":"text2", 
     "date":null, 
     "like":0, 
     "comments":[ 
      {"comment":"Yorum 4", "like":500}, 
      {"comment":"Yorum 5", "like":600} 
     ] 
    } 
     ] 
}; 
return obj; 

PostCommand

$scope.posts = PostFactory.posts; 
$scope.PostCommand = function(){ 
    $scope.posts.comments.push({ 
     "comment":$scope.comment, 
     "like":0 
    }); 
}; 

如何添加評論?如果用戶選擇哪一個添加到它 我使用路由器也許$ stateParams幫助我?

+0

這在很大程度上取決於你如何顯示這些數據上。問題與現在相比過於寬泛。顯示更多與查看和路由相關的代碼 – charlietfl

+0

https://plnkr.co/edit/pMo4g9jBAn2YDnh04JHv – ooz

+0

如果沒有真實模板,演示並不是很有幫助 – charlietfl

回答

1

我做如下:

<body ng-app="app"> 
    <div ng-controller="mainCtrl"> 
     <div ng-repeat="(key, post) in posts"> 
     {{post.text}} 
     {{post.date}} 
     <a href="#" ng-click="postLike(key)">Like {{post.like}}</a> 
     <form> 
      <input type="text" ng-model="newcomment[key].comment"> 
      <button ng-click="postCommand(key)">Add Comment</button> 
     </form> 
     <ul> 
      <li ng-repeat="(keyC, item) in post.comments"> 
       {{item.comment}} <br/> 
       <a href="#" ng-click="commentLike(key, keyC)"><i class="glyphicon glyphicon-thumbs-up"></i> Like {{item.like}}</a> 
      </li> 
     </ul> 
     </div> 
    </div> 
    </body> 

功能:

var app = angular.module('app', []); 
app.controller("mainCtrl", function($scope){ 

    $scope.posts = [ 
     { 
      "text":"text", 
      "date":null, 
      "like":0, 
      "comments":[ 
       {"comment":"Yorum 1", "like":100}, 
       {"comment":"Yorum 2", "like":200}, 
       {"comment":"Yorum 3", "like":300} 
      ] 
     }, 
     { 
      "text":"text2", 
      "date":null, 
      "like":0, 
      "comments":[ 
       {"comment":"Yorum 4", "like":500}, 
       {"comment":"Yorum 5", "like":600} 
      ] 
     } 
    ]; 

    $scope.postLike = function(key) { 
     var like = $scope.posts[key].like; 
     like = ($scope.posts[key].like == 1) ? 0 : 1 ; 
     $scope.posts[key].like = like; 
    }; 

    $scope.newcomment = {}; 
    $scope.postCommand = function(key){ 
     $scope.posts[key].comments.push($scope.newcomment[key]); 
     $scope.newcomment = {}; 
    }; 

    $scope.commentLike = function(key, keyC) { 
     var like = $scope.posts[key].comments[keyC].like; 
     like = ($scope.posts[key].comments[keyC].like == 1) ? 0 : 1 ; 
     $scope.posts[key].comments[keyC].like = like; 
    }; 
}); 
+0

謝謝。我如何顯示評論? – ooz

+0

很抱歉,但我必須添加「like」與評論 – ooz

+0

我會嘗試裝載一個例子,只需一分鐘! –