更改另一个控制器的下拉列表的值

时间:2015-08-28 14:31:55

标签: html angularjs drop-down-menu angularjs-scope

我正在填写下拉列表的json。 首先,下拉菜单的第一项是json数据的第一项。 但我需要通过另一个coontroller的$ scope变量更新元素。这个$ scope变量是从服务调用获取数据。 下拉菜单代码 -

<div class="col-sm-6">
    <select ng-model="funcLoc1item" style="width:63%;text-align:center;" ng-change="onchangefuncLoc1()"  ng-options="item.firstName as item.firstNamefor item in funcLoc1List">
    </select>
</div>

同一页面的控制器代码,其中包含下拉列表。

var funcLoc1 = {[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]};

$scope.funcLoc1List=funcLoc1;
$scope.funcLoc1item=$scope.funcLoc1List[0].firstName;
$rootScope.funcLoc1item=$scope.funcLoc1List[0].firstName;

$scope.onchangefuncLoc1 = function() {

    var selectedValue = $scope.funcLoc1item;
    $rootScope.funcLoc1item = selectedValue;
    alert(selectedValue);
};

直到现在一切都很好。但是现在我想从另一个控制器传递一个$ scope变量。这个变量现在应该是下拉列表的默认元素。 例如,在默认情况下,当页面加载 John 时,代码将是dropdown中的默认元素。过了一段时间后,我会在另一个控制器的变量中得到一些值,例如我得到了$ scope.xyz =& #34; anna&#34;。现在我需要将下拉列表的默认元素更改为动画片列表的第2项。

这是demo.i的链接,想要按下按钮点击的值,在下拉列表中设置默认值。

TRYING LINK

0 个答案:

没有答案