m1crdy
m1crdy

Reputation: 1401

Angular.js pass value from select to another view

I want to pass some selected value to another view. This is my HTML. There is a select field. On Button click the next page should be shown with the selected value as header title.

<div class="list">
  <label class="item item-input item-select">
    <div class="input-label">
      Deine Stadt
    </div>
    <select ng-model="selectedCity" ng-options="city.name for city in cities">

    </select>
  </label>
</div>

<br>
<button ng-click="searchCity(selectedCity)" class="button button-outline button-positive">
  Gutscheine suchen
</button>

The Controller:

.controller('AccountCtrl', function($scope, $state) {
    $scope.cities = [
      {id:'001', name:'Konstanz'},
      {id:"002", name:"Freiburg"} 
    ];
    $scope.selectedCity = $scope.cities[0];

    $scope.searchCity = function(selectedCity){
        console.log(selectedCity.name);
        $state.go("tab.friends");
    };
});

The console.log(selectedCity.name) works fine. But how can I bring this value to another controller to show it in header? I don´t need a service! Ty.

UPDATE: The VIEW where the title should be shown:

<ion-view title="{{selectedCity.name}}">
</ion-view>

The Controller of the second view:

.controller('FriendsCtrl', function($scope) {

})

Upvotes: 2

Views: 5417

Answers (3)

Michael Kang
Michael Kang

Reputation: 52847

You can define a scope variable higher up in the scope chain so that the model is shared between both controllers:

<body ng-controller="bodyCtrl">

    <ion-view title="{{model.selectedCity.name}}">
    </ion-view>

    <div ng-controller="AccountCtrl">
    <div class="list">
      <label class="item item-input item-select">
        <div class="input-label">
          Deine Stadt
        </div>
        <select ng-model="model.selectedCity" ng-options="city as city.name for city in cities">   
        </select>
      </label>
    </div>

    <br>
    <button ng-click="searchCity(model.selectedCity)" class="button button-outline button-positive">
      Gutscheine suchen
    </button>
    </div>
</body>

Parent Controller

app.controller('bodyCtrl', function($scope) {
    $scope.model = { selectedCity: }
});

Account Controller

.controller('AccountCtrl', function($scope, $state) {
    $scope.cities = [
      {id:'001', name:'Konstanz'},
      {id:"002", name:"Freiburg"} 
    ];
    $scope.model.selectedCity = $scope.cities[0];

    $scope.searchCity = function(selectedCity){
        console.log(selectedCity.name);
        $state.go("tab.friends");
    };
});

Upvotes: 0

Shujaath Khan
Shujaath Khan

Reputation: 1384

when('/selectedCity/:CityId', {
      templateUrl: 'templates/CityDetails.html',
      controller: 'ShowOrderController'
});

and in Controller

$scope.order_id = $routeParams.CityId;

Upvotes: 1

Manish Kr. Shukla
Manish Kr. Shukla

Reputation: 4477

You can pass the value as a route params to the other view, provided if the other view points to some other route..

in your routes.js, you can put it like viewName/:paramName.

And then in your controller, you can access it like $routeParams.paramName..

[paramName is the name of the parameter you're passing :D]

Upvotes: 1

Related Questions