Я не знаю, как получить доступ к данным из вложенного (дочернего) контроллера.
<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 контроллера. Основная форма - это форма, вторая - поле ввода, которое позволяет пользователю искать имя в списке с помощью typeahead.
Я хочу иметь доступ к содержимому ng-model = "name" в моем дочернем контроллере (mainctrl) в моем TestController.
Я попытался получить к нему доступ напрямую с $ scope.name, но он не работает. Я также попробовал test.name как ng-model, и он тоже не работал.
Я отправляю данные своего TestController на свой сервер и хочу отправить данные (имя) из моего mainCtrl, а также непосредственно из TestController. Поэтому, когда мой пользователь нажимает кнопку отправки, он отправляет имя + test_content в запрос $ http.post.
Кто-нибудь знает, как это сделать?
благодаря
Я нашел это, но это действительно не помогло. 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';
});
};
}]);
Область родителя доступна изнутри дочерней области, но дочерняя область недоступна из родителя. Как правило, это относится к назначению значений из области дочерних объектов свойствам, уже определенным в родительском. В вашем случае, я думаю, вам просто нужно использовать test.name в качестве выражения ng-model.