我是新手开发一个Angular应用程序。
我有两种观点:
搜索器表单位于视图中,并且包含在(主视图和结果)视图中。两个视图都链接到同一个控制器。
问题是,当我在主视图中单击搜索时,将调用服务并返回值,但结果不会显示在结果视图中。
如果我在结果视图中执行相同的查询,则一切正常并显示结果。
代码:
主视图和结果包含完全相同的seacher视图:
<div ng-include src="'partials/boat-searcher.html'" class="text-center"></div>
搜索者观点:
<input type="text" ng-model="departure" typeahead="suggestion for suggestion in cities($viewValue)" class="input-xlarge input-height-large" placeholder="Salida" autocomplete="off">
我的应用:
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']).
config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'BoatListCtrl'});
$routeProvider.when('/option-list', {templateUrl: 'partials/option-list.html', controller: 'BoatListCtrl'});
}]);
我的控制器:
.controller('BoatListCtrl', function BoatListCtrl($scope, $location, $routeParams, $http, Boat, Equipment, Extra) {
// Departures typeahead
$scope.departure = undefined;
$scope.cities = function(cityName) {
return $http.get("http://localhost:3000/departures?query="+cityName).then(function(response){
var names = response.data.map(function (source) { return source.name; });
return names;
});
};
// Gets options (boats)
$scope.getOptionList = function(departure, fechaSalida, fechaLlegada, personas) {
$scope.boats = Boat.query({departure: departure, fechaSalida: fechaSalida, fechaLlegada: fechaLlegada, personas: personas});
$location.path('/option-list');
};
答案 0 :(得分:0)
我终于自己解决了。所以,我将解释我是如何做到的:
使用查询表单查看:
<form class="form-search">
<input type="text" ng-model="departure" typeahead="suggestion for suggestion in cities($viewValue)" class="input-xlarge input-height-large" placeholder="Salida" autocomplete="off">
// Your form query fields
<button class="btn btn-large btn-primary" ng-click="getOptionList(departure, fechaSalida, fechaLlegada, personas)"><i class="icon-search icon-white icon-small"></i> Buscar</button>
在我的HomeCtrl中(这是我查询的控制器,但我想在另一个视图中显示另一个控制器:
.controller('HomeCtrl', function HomeCtrl($scope, $location, $http, $filter, Boat, BoatService, SearcherService) {
// Gets options (boats)
$scope.getOptionList = function(departure, departureDate, arrivalDate, people) {
$scope.formattedDepartureDate = $filter('date')(departureDate,'yyyy-MM-dd');
$scope.formattedArrivalDate = $filter('date')(arrivalDate,'yyyy-MM-dd');
$scope.people = people;
$scope.boats = Boat.query({departure: departure, departureDate: $scope.formattedDepartureDate, arrivalDate: $scope.formattedArrivalDate, people: $scope.people});
BoatService.setBoats($scope.boats);
$location.path('/option-list');
};
})
注意:重要的是要注意,我获得了一个范围变量的船只列表,我将它设置在一个带有BoatService.setBoats的服务中(此服务将可用于下一个控制器以获取船只。)
我的boat-list.html视图
<div class="row-fluid" ng-repeat='boat in boats'>
{{boat.name}}
...
BoatListCtrl(在另一个视图中显示船只):
.controller('BoatListCtrl', function BoatListCtrl($scope, $location, $http, $filter, SearcherService, Boat, BoatService, Equipment, Extra,
Rating, Comment, Booking, BookingService) {
$scope.boats = BoatService.getBoats();
};