我使用以下版本:
> Ionic,v1.0.0-beta.14
> AngularJS v1.3.6
路线配置:
myApp.config(function ($stateProvider) { $stateProvider .state('manager_add',{ url: '/managers/add',templateUrl: 'app/components/mdl.role_members/views/add.html',controller: 'ManagerAddController' }); });
控制器配置:
myApp.controller('ManagerAddController',function ($scope,$state,$ionicLoading,$filter,ContactService,ManageRSService,RoleRequest,RoleRequestsSentService,ToastrService) { $scope.role_request = RoleRequest.new(); $scope.showContactSearch = false; $scope.managers = ManageRSService.collection(); $scope.$watchCollection("managers",function( newManagers,oldManagers ) { if(newManagers === oldManagers){ return; } $scope.managers = newManagers; $scope.contactsToBeInvited = getNotInvitedContacts(); }); $scope.contacts = ContactService.collection(); $scope.$watchCollection("contacts",function( newContacts,oldContacts ) { if(newContacts === oldContacts){ return; } $scope.contacts = newContacts; $scope.contactsToBeInvited = getNotInvitedContacts(); }); $scope.contactsToBeInvited = getNotInvitedContacts(); function getNotInvitedContacts() { var notinvited = []; angular.forEach($scope.contacts,function(contact) { if(angular.isObject($scope.managers)) { var results = $filter('filter')($scope.managers,{member_id: Number(contact.contact_id)},true); if (results.length == 0) { this.push(contact); } } else { this.push(contact); } },notinvited); return notinvited; } $scope.search_contact = ""; $scope.search = function(contact) { if($scope.search_contact === "" || $scope.search_contact.length === 0) { return true; } $scope.showContactSearch = true; var found = false; if(contact.display_name) { found = (contact.display_name.toLowerCase().indexOf($scope.search_contact.toLowerCase()) > -1); if(found) { return found; } } if(contact.contact.getFullName()) { found = (contact.contact.getFullName().toLowerCase().indexOf($scope.search_contact.toLowerCase()) > -1); if(found) { return found; } } if(contact.contact.email) { found = (contact.contact.email.toLowerCase().indexOf($scope.search_contact.toLowerCase()) > -1); if(found) { return found; } } return found; } $scope.selectContact = function(contact) { $scope.search_contact = contact.contact.getFullName(); // TODO: Make dynamic role $scope.role_request.role_id = 4; $scope.role_request.email = contact.contact.email; }; $scope.addRoleMember = function(valid) { if($scope.role_request.email === "") { return; } if(!valid) { return; } $ionicLoading.show({ template: 'Please wait...' }); RoleRequestsSentService.add($scope.role_request).then(function(roleJsonResponse){ ToastrService.toastrSuccess('Request send','We have send an invite to '+ $scope.search_contact +'.'); $ionicLoading.hide(); $state.go('managers'); }); } });
查看配置:
<ion-view view-title="ManagerAdd" > <ion-content class="has-header scroll="true"> <div class="content"> <div class="list"> <div class="item item-border"> <p>Some text</p> </div> </div> <form name="managerForm"> <div class="list"> <div class="item item-divider"> Some text </div> <div class="item item-border"> <form name="fillForm"> <div class="form-group"> <label class="item item-input item-stacked-label item-textarea"> <span class="input-label border-none">Personal message: <span class="text-red required">*</span></span> <textarea name="message" ng-model="role_member.message" required></textarea> </label> <p ng-show="managerForm.message.$dirty && managerForm.message.$error.required" class="error-message">Message required!</p> </div> <div class="form-group"> <label class="item item-input"> <span class="input-label">Search on name <span class="text-red required">*</span></span> <input type="text" name="search_contact" ng-model="$parent.search_contact"> </label> <div class="searchResultBox" ng-show="showContactSearch"> <ion-scroll direction="y" class="scrollArea"> <div class="list"> <a class="item item-border item-avatar pointer" ng-repeat="contact in $parent.filteredContacts = (contactsToBeInvited | filter:search:false)" ng-click="$parent.selectContact(contact)"> <img src="{{ contact.getImage('thumbnail') }}"> <h2>{{contact.getIconName()}}</h2> <p>City: {{contact.contact.city}}</p> </a> </div> </ion-scroll> <div class="notFound pointer" ng-hide="filteredContacts.length"> <h3>Nobody found</h3> <p>You can only search through existing contacts</p> </div> </div> </div> </form> </div> </div> <div class="form-actions"> <button type="submit" class="button button-block regie-button" ng-click="addRoleMember(registerForm.$valid)"> Sent request </button> </div> </form> <p class="text-red" style="text-align:center; font-size:14px; font-weight: 400;">* required</p> </div> </ion-content> </ion-view>
正如您在视图中看到的,我需要在以下字段中使用$parent来使其工作:
> ng-model =“$parent.search_contact”
> ng-repeat =“联系$parent.filteredContacts =(contactsToBeInvited | filter:search:false)”
> ng-click =“$parent.selectContact(contact)”
我真的不明白为什么这是必要的,因为完整的视图使用相同的控制器.有没有人有想法?
问题是继承.在控制器的范围和那些字段之间,有几个新的范围(离子含量,离子滚动和可能的其他).
原文链接:https://www.f2er.com/angularjs/143370.html例如,ng-model =“search_content”.当你在那里写作时,它将在离子内容范围内创建一个search_content变量(如果有任何我没有看到的话,则在中间范围内创建).由于search_content是在离子内容中创建的,因此您的控制器将无法看到它.
如果你执行$parent.search_content,它将在父内容中创建它(AKA是控制器的范围).
你不应该这样做,今天$parent为你提供什么,明天它可以指向其他任何东西(因为你可以在不知道它的情况下添加一个新的范围,因此$parent将指向离子内容.
因此,您需要使用对象而不是基元,而不是这样做,例如:
ng-model="form.search_contact"
感谢,它将通过原型链查找表单对象,直到它在控制器的范围内找到它并使用它(正好你需要的).
阅读this比我的解释好一百倍.