gpt4 book ai didi

angularjs - 为什么使用 ion-scroll 时需要 $parent 来启用 ng-click 中的功能?

转载 作者:行者123 更新时间:2023-12-02 06:21:39 25 4
gpt4 key购买 nike

我使用以下版本:

  1. Ionic,v1.0.0-beta.14
  2. 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'
});
});

Controller 配置:

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>

正如您在 View 中看到的,我需要在以下字段中使用 $parent 才能使其正常工作:

  • ng-model="$parent.search_contact"
  • ng-repeat="$parent.filteredContacts 中的联系人 = (contactsToBeInvited | filter:search:false)"
  • ng-click="$parent.selectContact(contact)"

我真的不明白为什么这是必要的,因为完整的 View 使用相同的 Controller 。有人有想法吗?

最佳答案

这是一个非常典型的 Angular 范围问题。 Ion-view 创建一个新的子作用域并使用原型(prototype)继承:https://github.com/angular/angular.js/wiki/Understanding-Scopes

三种解决方法有:

关于angularjs - 为什么使用 ion-scroll 时需要 $parent 来启用 ng-click 中的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com