2015-09-23 47 views
0

我不知道如何从嵌套(子)控制器访问数据。角度嵌套控制器数据访问

<form ng-controller="TestController as test" ng-submit="submit()"> 
     <h5>Employee name :</h5> 
     <div ng-controller="mainCtrl" class="row-fluid"> 
      <form class="row-fluid"> 
       <div class="container-fluid"> 
        <input type="text" ng-model="name" typeahead="name for name in names | filter:$viewValue" /> 
       </div> 
      </form> 
     </div> 
     <h5>Comment : </h5> 
     <textarea ng-model="test.test_content"></textarea> 
     <input type="submit" id="submit" value="Submit" /> 
    </form> 

正如你可以看到我有2 controllers.The主要原因之一是一种形式,第二个是一个输入框,允许用户在使用预输入一个搜索列表的名称。

我想能够在我的TestController中访问我的子控制器(mainctrl)中的ng-model =“name”的内容。

我试图用$ scope.name直接访问它,但它不起作用。 我也尝试过使用ng-model作为test.name,它也没有工作。

我将我的TestController的数据发送到我的服务器,并希望直接从TestController从我的mainCtrl以及发送数据(名称)。因此,当我的用户单击提交时,它会在$ http.post请求中发送名称+ test_content。

任何人都知道该怎么做?

感谢

我发现这一点,但它并没有真正帮助.. https://fdietz.github.io/recipes-with-angular-js/controllers/sharing-models-between-nested-controllers.html

编辑:

我的搜索控制器

  .controller("mainCtrl", function ($scope, $http) { 
     $scope.selected = ''; 
     $http.get('/feedbacks/search.json'). 
     then(function(response) { 
     $scope.succes = " ok " 
     $scope.names = response.data; 
     // this callback will be called asynchronously 
     // when the response is available 
     }, function(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
      $scope.succes = " error" 
     }); 
    }); 

我的表单控制器:

angular.module('FeedbackTool') 
    .controller('TestController',['$scope', '$http', function($scope,$http) { 
     $http.get('/feedbacks.json'). 
         then(function(response) { 
          $scope.succes = " ok " 
          $scope.list = response.data; 
          // this callback will be called asynchronously 
          // when the response is available 
         }, function(response) { 
          // called asynchronously if an error occurs 
          // or server returns response with an error status. 
                 $scope.succes = " error" 
         }); 
     $scope.submit = function() { 
       $http.post('/feedbacks.json', { data:this.test }). 
         then(function(response) { 
       $scope.succes = 'sent'; 
          // this callback will be called asynchronously 
          // when the response is available 
         }, function(response) { 
          // called asynchronously if an error occurs 
          // or server returns response with an error status. 
              $scope.succes = 'fail'; 

         }); 
      }; 
    }]); 
+0

你可以发布你的控制器代码吗? –

回答

0

可以从子范围内访问父范围,但父范围无法访问子范围。通常情况下,处理这种情况的方法是将子范围的值分配给父代中已经定义的属性。在你的情况下,我认为你只需要使用test.name作为ng模型表达式。

+0

是的,我已经测试过,它不工作。其实我的表单并没有被完全提交..所以我想我最好的选择是将两个控制器合并为一个,这样我就不会再发生这个问题了,但是看起来会很脏 – jayD

+0

没有控制器代码,它很难告诉问题是什么。在你不想要的体系结构之前,我建议检查一下:http://www.angularjshub.com/examples/basics/nestedcontrollers/ –

+0

我只是用两个控制器更新我的文章。为了清楚起见,我将它分成2个控制器我真的不认为会有问题tbh,感谢看起来有帮助的链接 – jayD