Angularjs 角度控制器不´;不要显示数据

Angularjs 角度控制器不´;不要显示数据,angularjs,Angularjs,我是开发角度应用程序的新手 我有两种看法: 主:搜索元素 结果:显示查询结果 searcher表单位于一个视图中,它包含在(主视图和结果视图)中。两个视图都链接到同一控制器 问题是,当我在主视图中单击search时,会调用服务并返回值,但结果不会显示在results视图中 如果我在results视图中执行相同的查询,则一切正常,并显示结果 守则: 主视图和结果包括完全相同的seacher视图: <div ng-include src="'partials/boat-searcher.htm

我是开发角度应用程序的新手

我有两种看法:

  • 主:搜索元素
  • 结果:显示查询结果
  • searcher表单位于一个视图中,它包含在(主视图和结果视图)中。两个视图都链接到同一控制器

    问题是,当我在主视图中单击search时,会调用服务并返回值,但结果不会显示在results视图中

    如果我在results视图中执行相同的查询,则一切正常,并显示结果

    守则:

    主视图和结果包括完全相同的seacher视图:

    <div ng-include src="'partials/boat-searcher.html'" class="text-center"></div>
    
    我的控制器:

    .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');                                         
            };
    
    .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');                                         
        };
    })
    

    我终于自己解决了。因此,我将解释我是如何做到这一点的:

  • 使用查询表单查看:

    <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>                
    
  • 注意:这里需要注意的一点很重要,我将船只列表设置为一个范围变量,并使用BoatService.setBoats在服务中进行设置(下一个控制器可以使用此服务来获取船只)

  • My boat-list.html视图

    <div class="row-fluid" ng-repeat='boat in boats'>
      {{boat.name}}
      ...
    
  • .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');                                         
        };
    })
    
    <div class="row-fluid" ng-repeat='boat in boats'>
      {{boat.name}}
      ...
    
     .controller('BoatListCtrl', function BoatListCtrl($scope, $location, $http, $filter, SearcherService, Boat, BoatService, Equipment, Extra, 
                                                      Rating, Comment, Booking, BookingService) {               
    
        $scope.boats = BoatService.getBoats();
      };