我不知道如何从嵌套(子)控制器访问数据。角度嵌套控制器数据访问
<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';
});
};
}]);
你可以发布你的控制器代码吗? –