我發現了類似的問題,我被問及被回答,但我認爲我的情況是不同的。我從示例代碼enter link description here開始了一個帶有使用Google地圖的滑動菜單的簡單應用程序。在OnsenUI + AngularJS + PhoneGap控制器之間傳遞數據
我正在創建一個原型,讓用戶提交添加到待處理項目列表的新項目,然後添加到地圖中。我現在專注於第一部分 - 讓用戶創建項目,並自動更新未決提交列表。然後我添加了一個簡單的表格,創建新的項目:
<ons-page>
<ons-toolbar fixed-style ng-controller="SlidingMenuController">
<div class="left">
<ons-toolbar-button ng-click="slidingMenu.toggleMenu()"><ons-icon icon="bars"></ons-icon></ons-toolbar-button>
</div>
<div class="center">New Submission</div>
</ons-toolbar>
<ons-row style="margin-top: 10px; text-align: center;">
<ons-col ng-controller="NewSubmissionController">
<p style="font-size: 12px;">Please insert a brief description of your find and its material.</p>
<textarea style="width: 97%;" id="subDescrText" class="textarea" rows="4" placeholder="Type your description here..." value="" ng-model="subDescription"></textarea>
<ons-button style="margin-top: 24px;" ng-click="doStore()">
Store
</ons-button>
</ons-col>
</ons-row>
</ons-page>
而另一頁列出創建提交:
<ons-page>
<ons-toolbar>
<div class="left">
<ons-toolbar-button ng-click="slidingMenu.toggleMenu()"><ons-icon icon="bars"></ons-icon></ons-toolbar-button>
</div>
<div class="center">Pending Submissions</div>
</ons-toolbar>
<div style="text-align: center;">
<ons-list id="pendingList" var="aPendingList" ng-controller="PendingListController">
<ons-list-item ng-repeat="pi in pendingItems">
<p>{{pi.description}}</p>
<span>{{pi.material}}</span>
</ons-list-item>
</ons-list>
</div>div>
<p style="margin-top: 12px; text-align: center; width: 100%;" >
<ons-button ng-click="">
Upload All
</ons-button>
</p>
</ons-page>
我加入這些控制器:
app.controller('NewSubmissionController', function($scope) {
$scope.selMat;
$scope.subDescription;
$scope.doStore = function() {
alert('In NewSubmissionController.doStore() - Sel material: ' + $scope.selMat + '\nDescr: ' + $scope.subDescription);
var newPI = new SubmissionItem($scope.selMat, $scope.subDescription, '');
$scope.$emit('newPI', newPI);
slidingMenu.setMainPage('pendingList.html', {closeMenu: true});
};
});
// Pending list controller
app.controller('PendingListController', function($scope) {
$scope.pendingItems = [];
$scope.$on('newSubEvent', function(e, subMat, descr) {
alert('In PendingListController, event newSubEvent - sub mat: ' + subMat + '\nDescription: ' + descr);
});
$scope.addPendingItem = function(newPi) {
alert('In PendingListController.addPendingItem() - ' + newPi);
$scope.pendingItems.unshift(newPi);
// Some code here to update the list of pending submissions...
};
});
在這個版本正如其他答覆所建議的,我試圖使用事件系統。不幸的是,它不起作用,因爲兩個控制器不是孩子和父母。移動PendingListController裏面NewSubmissionController也不起作用,我估計這是因爲兩個控制器在兩個不同的視圖。
什麼是從NewSubmissionController調用PendingListController.addPendingItem()的最佳解決方案?
只是爲了讓我得到它 - 因爲在我的情況下,我有滑動菜單控制器在主導航頁面中,我應該定義我的_NewSubmissionController_和_PendingListController_作爲其子控制器,以便他們可以使用作爲根的滑動菜單控制器的範圍範圍? – Btz
我重構了我的代碼,遵循你的例子,它工作。我很感激,但我想更好地理解。它爲什麼有效?控制器沒有被定義爲任何其他的子項,那麼rootScope是什麼?順便說一下,有沒有什麼好的教程/文檔涉及這些主題?我可以關注並理解Angular的網站上的基本內容,但它比這些主題更早停止。 – Btz
這取決於您將控制器放在html模板中的位置。簡而言之,作用域是從父視圖繼承的:(rootScope - >父作用域 - >子作用域),因此子仍然可以訪問其父作用域。有關更詳細的解釋,我發現[角度範圍繼承](https://github.com/angular/angular.js/wiki/Understanding-Scopes#angular-scope-inheritance)有很好的示例和圖表,您可以按照。希望能幫助到你。 –