Angular.js将值从select传递到另一个视图

时间:2014-08-04 09:28:52

标签: javascript angularjs

我想将一些选定的值传递给另一个视图。这是我的HTML。有一个选择字段。在按钮上单击,应显示下一页,并将所选值作为标题标题。

<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>

控制器:

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

console.log(selectedCity.name)运行正常。但是,如何将此值传递给另一个控制器以在标题中显示它?我不需要服务! TY。

更新:应显示标题的VIEW:

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

第二个视图的控制器:

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

})

3 个答案:

答案 0 :(得分:1)

如果另一个视图指向其他路径,您可以将该值作为路径参数传递给另一个视图。

在您的routes.js中,您可以将其设置为viewName /:paramName。

然后在您的控制器中,您可以像$ routeParams.paramName ..

一样访问它

[paramName是您传递的参数的名称:D]

答案 1 :(得分:1)

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

和控制器

$scope.order_id = $routeParams.CityId;

答案 2 :(得分:0)

您可以在范围链中更高的范围内定义范围变量,以便在两个控制器之间共享模型:

<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>

家长控制器

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

帐户控制器

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