2015-03-25 6 views
1
app.controller('FilterController', ['$scope', '$http', 
function($scope,$http) { 

    //Loading the data to the filter scope 
    $http.get('/main').success(function(response){ 
     $scope.data = response; 
    }); 

    //The object that the input fields in the modal bind to 
    $scope.selected = {}; 

    this.applyFilter = function(){ 
     $http.post('/main/query', $scope.selected).success(function(response){ 
      //The response is the filtered object sent by the server 
      console.log(response); //This is the response I want to bind to the main view 

      //Do something to pass the response to the main scope 

     }); 
    }; 
}]); 

Модаль включает в себя некоторые выпадающие списки для пользователей, которые выбирают параметры, и эти параметры сохраняются в «выбранной» переменной, которая, в свою очередь, отправляется в базу данных для запроса нового набора данных. Теперь задача состоит в том, чтобы отправить эти новые данные в основную область и обновить страницу. Я провел некоторое исследование и обнаружил, что кажется, что это можно сделать по разрешению, но я точно не знаю, как скомпоновать код. Пожалуйста, помогите ..Как отправить данные, полученные с модального на главный вид?

ответ

0

Поскольку вы помечено вопрос с угловым-ш я буду предположим, что вы используете ui.bootstrap для модальностей.

Сначала введите ui.bootstrap в свое приложение.

В вашем основном контроллере вы открываете модальность:

app.controller('MainController', ['$scope', '$modal', 
function($scope,$modal) { 
    $scope.filterModal = $modal.open({ 
     templateUrl: 'modal.html', 
     controller: 'FilterController', 
     size: 'md' 
    }); 
    $scope.filterModal.result.then(function (result) { 
     // do what you have to with result from modal 
    }); 
}]); 

Вашего модальным должны иметь контроллер:

app.controller('FilterController', ['$scope', '$http','$modalInstance', 
function($scope,$http, $modalInstance) { 

    //Loading the data to the filter scope 
    $http.get('/main').success(function(response){ 
     $scope.data = response; 
    }); 

    //The object that the input fields in the modal bind to 
    $scope.selected = {}; 

    this.applyFilter = function(){ 
     $http.post('/main/query', $scope.selected).success(function(response){ 
      //The response is the filtered object sent by the server 
      console.log(response); //This is the response I want to bind to the main view 

      //Do something to pass the response to the main scope 
      $modalInstance.close(response); 
     }); 
    }; 
}]); 
+0

пошел назад и изучить пример снова и, наконец, понять, что это $ modalInstance.close(), который делает магию, спасибо человеку! – user2901633

0

Вы должны иметь возможность сделать это, просто присвойв $ scope.data ответ.

this.applyFilter = function(){ 
     $http.post('/main/query', $scope.selected).success(function(response){ 
      //The response is the filtered object sent by the server 
      console.log(response); //This is the response I want to bind to the main view 

      // Binding the response 
      $scope.data = response; 

      //Do something to pass the response to the main scope 

     }); 
    };