2016-12-01 202 views
-1

在下面的代碼從我的應用程序,下拉沒有得到與範圍數據更新。我該如何做這項工作?我需要將下拉選項從父控制器傳遞給子控制器。我得到[$ injector:modulerr]錯誤。ng選項不更新選擇標籤

<div ng-app="myApp" ng-controller="parentCtrl as pt"> 
    <button ng-click="pt.callChild()">Change Dropdown</button> 
    <div ng-controller="childCtrl as ct"> 
     <select ng-model="ct.selectedName" ng-options="item for item in ct.names"> 
     </select> 
    </div> 
</div> 

JS:

var app = angular.module('myApp', []); 

app.controller('parentCtrl', function ($scope) { 
    var vm = this; 
    var newArray = ["Steve", "Jony"]; 
    vm.callChild = function() { 
    $scope.$broadcast('someEvent', newArray); 
    }; 
}); 

app.controller('childCtrl', function ($scope) { 
    var vm = this; 
    vm.names = ["Emil", "Tobias", "Linus"]; 

    $scope.$on('someEvent', function (e, newArray) { 
     vm.names = newArray; 
    }); 
}); 

這裏是fiddle

+0

如果初始綁定不工作,這並不奇怪,整個事情是行不通的。你能發佈整個錯誤信息嗎? – valepu

+0

您可以通過在小提琴中打開控制檯來查看消息。但是在這裏發佈它:VM66 angular.min.js:6未捕獲的錯誤:[$ injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=myApp&p1=Error%3A %2 ... ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.8%2Fangular.min.js%3A20%3A274)(...) – maverick

+0

對不起,當我寫評論時沒有注意到提琴 – valepu

回答

1

該代碼工作正常,我已合併角1.5版。

請參閱plnkr here

<head> 
    <meta charset="utf-8" /> 
    <title>AngularJS Plunker</title> 
    <script> 
    document.write('<base href="' + document.location + '" />'); 
    </script> 
    <link rel="stylesheet" href="style.css" /> 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script> 
    <script src="app.js"></script> 
</head> 



<div ng-app="myApp" ng-controller="parentCtrl as pt"> 
    <button ng-click="pt.callChild()">Change Dropdown</button> 
    <div ng-controller="childCtrl as ct"> 
    <select ng-model="ct.selectedName" ng-options="item for item in ct.names"> 
    </select> 
    </div> 
</div> 

</html>