0

看法:

  <ul ng-repeat="x in posts.post">
      {{x.name}}  {{x._id}} {{x.post}} {{x.user_id}} 
      <br>
      <ul ng-repeat="y in x.comment">
        {{y.comment}}
      </ul>
      <input type="text" style="display: none;" ng-model='new_comment.userId' value={{users2.id}} name="userId"  >
      <input type="text" style="display: none;" ng-model='new_comment.name' value={{users2.name}} name="userName"   >
      <textarea ng-model='new_comment.comment' name="comment" rows="4" cols="50">
      </textarea> 
      <br>
      <input type="submit" value="Post Comment!" ng-click="addComment(x._id, new_comment)">
  </ul>

控制器:

UserFactory.getUser(function (data) {
    $scope.users2 = data;
});

工厂:

factory.getUser = function(callback) {
    $http.get("/user").success(function(output) {
        users2 = output;
        callback(users2);
    });
};

我正在尝试将 users2.id 和 users2.name 的隐藏值从控制器/工厂传递到表单中。我尝试了 ng-init、ng-value 以及 input type="hidden",但都没有。

所以这就是我为让它工作所做的事情

看法:

  <form>

  <textarea ng-model='new_comment.comment' name="comment" rows="4" cols="50">
  </textarea> 

  <br>
  <input type="submit" value="Post Comment!" ng-click="addComment(x._id, new_comment, users2.name, users2._id)">
  </form>

控制器:

$scope.addComment = function(id, comment, userName, userId) {

var commentValue = comment.comment;

var newComment = {comment: commentValue, name: userName, userId: userId};

postFactory.addComment(id, newComment, function () {

    postFactory.getComment(function (data) {
        $scope.comments = data;
    });

    $scope.new_comment = {};
});
};
4

3 回答 3

1

尝试这个

  <input type="hidden" ng-model='new_comment.userId' value="{{users2.id}}" name="userId"  >

当你改变事物时

UserFactory.getUser
.then(function (data) {
    $scope.users2 = data;
});

factory.getUser = function() {
    return $http.get("/user");
};

因为 $http 返回一个承诺

于 2015-11-02T19:58:40.017 回答
1

两种方式绑定不适用于hidden元素,所以我会ng-value用来设置type="hidden"元素的值

<input type="hidden" ng-value='new_comment.userId' name="userId"/>
<input type="hidden" ng-value='new_comment.name' name="userName"/>
于 2015-11-02T20:04:37.400 回答
0

做这样的事情,回报承诺。

factory.getUser = function(callback) {
    return $http.get("/user")
};

控制器:

UserFactory.getUser().success(function(output) {
    $scope.users2 = output.data;
});
于 2015-11-02T19:57:05.253 回答