Patrick Patrick - 2 years ago 155
jQuery Question

Pass data from modal to main controller- (Plunker attached)----contains autocomplete input box inside a modal

My situation is slightly different than passing normal data from modal to main controller. my input field in modal contains an autocomplete box.

I have the following plunker attached herewith

$scope.ok = function() {

Once user clicks on 'Add User', a modal opens. We start entering name in the first input field. if we type letter 'a', autocomplete feature shows the options. We select Angular and select 'Scope' and 'Watch' from other 2 dropdown input values.

Now, we press 'ok'. Can someone tell me how to pass the above selected values in the modal to the controller.

Answer Source

You can use AngularStrap modals, which I personally prefer because the data manipulation is easier, in my opinion.

Here's code:


angular.module('app', ['ngAnimate', 'ngSanitize', 'mgcrea.ngStrap'])

.config(function($modalProvider) {
  angular.extend($modalProvider.defaults, {
    html: true

.controller('mainCtrl', function($scope, $rootScope, $modal) {
  $scope.selectedState = '';
  $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];

  $scope.modal = {
    title: "<strong>Example</strong>",
    html: true,
    show: true

  $scope.get_change = function(value) {
      $scope.selectedState = value;


<!DOCTYPE html>
<html ng-app="app">

  <script src="" type="text/javascript"></script>
  <link href="" rel="stylesheet" />
  <script src="" type="text/javascript"></script>
  <script src=""></script>
  <script src=""></script>
  <script src="" data-semver="1.5.7"></script>
  <script src=""></script>
  <script src=""></script>
  <link href="" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
  <link rel="stylesheet" href="">
  <link rel="stylesheet" href="">

<body ng-controller="mainCtrl">
  <div class="col-md-6">
    <button type="button" class="btn btn-md btn-primary" data-animation="am-flip-x" data-template-url="modal/docs/modal.demo.tpl.html" data-placement="center" bs-modal="modal">Open modal
    <span ng-bind="selectedState"></span>


<!-- Modal content

<div class="modal ng-scope center am-fade-and-scale" tabindex="-1" role="dialog" aria-hidden="true" style="z-index: 1050; display: block;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header" ng-show="title">
        <button type="button" class="close" aria-label="Close" ng-click="$hide()"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" ng-bind-html="title"></h4></div>
      <div class="modal-body">
        <div class="form-group">
          <label><i class="fa fa-globe"></i> State</label>
          <input type="text" class="form-control" ng-model="selectedState" bs-options="state for state in states" placeholder="Enter state" ng-change="get_change(selectedState)" bs-typeahead>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" ng-click="$hide()">Close</button>


You can check the complete code in plnkr.

For more info about typeaheads in AngularStrap check here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download