我发现有人问我类似的问题,也有人回答,但我认为我的情况不同。我从这里的示例代码开始
enter link description here
一个使用谷歌地图的带有滑动菜单的简单应用程序。
我正在创建一个原型,允许用户提交添加到挂起项目列表中的新项目,然后添加到地图中。我现在专注于第一部分——让用户创建项目,并自动更新待提交的列表。然后我添加了一个简单的表单来创建新项目:
<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添加PendingItem()?