从父控制器对象分配ng-model属性

时间:2014-05-08 17:46:11

标签: angularjs angularjs-scope

我有使用ng-repeat列出的相关对象,它应该为单独的嵌套角度控制器生成一个表单。我试图使用父控制器的对象设置ng-model属性。

现在控制器看起来像:

wha.controller('ConnectionRequestsCtrl', function ($scope) {
  $scope.new_connection_request = {};

  $scope.create = function () {
    console.log($scope.new_connection_request);
  };
});

Console.log打印{}文字。如果我从我的控制器中删除new_connection_request的定义,我会得到未定义的。

我尝试将new_connection_request对象值分配给嵌套在名为contact_suggestion的相关对象内的表单。

//nested inside contact suggestion controller with contact_suggestion object

<form ng-controller="ConnectionRequestsCtrl" ng-submit="create()">
  <input ng-model="new_connection_request.sender_id" ng-value="contact_suggestion.user_id" type="hidden"></input>
  <input ng-model="new_connection_request.recipient_id" ng-value="contact_suggestion.connection_id" type="hidden"></input>
  <button class="btn" type="submit">Connect</button>
</form>

在生成的html中正确设置了表单值。

<form ng-controller="ConnectionRequestsCtrl" ng-submit="create()" class="ng-scope ng-pristine ng-valid">
  <input ng-model="new_connection_request.sender_id" ng-value="contact_suggestion.user_id" type="hidden" class="ng-pristine ng-valid" value="157">
  <input ng-model="new_connection_request.recipient_id" ng-value="contact_suggestion.connection_id" type="hidden" class="ng-pristine ng-valid" value="2154">
  <button class="btn" type="submit">Connect</button>
</form>

0 个答案:

没有答案