У меня есть массив в моей области, заполненный вызовом службы, вызванным с помощью ui-select
:
<ui-select search-enabled="true" ng-model="$parent.CurrentUser.ClubOrganization" ng-change="$parent.ClubOrganization_Change()" theme="select2" append-to-body="true"></ui-select>
$scope.TeamTypes = undefined;
$scope.ClubOrganization_Change = function () {
$scope.GetClubTeamTypes();
};
$scope.GetClubTeamTypes = function () {
$scope.GetTeamTypes($scope.CurrentUser.ClubOrganization.OrganizationId);
};
$scope.GetTeamTypes = function (organizationId) {
apiService.GetTeamTypes(organizationId).success(function (response) {
$scope.TeamTypes = response;
});
};
<div class="block block-bordered" ng-hide="{{$parent.TeamTypes.length == undefined}}">
<div class="block-header bg-gray-lighter">
<h3 class="block-title">Teams</h3>
</div>
<div class="block-content">
<div class="typeTeams">
<span ng-repeat="typeTeam in $parent.TeamTypes">
<md-checkbox aria-label="{{typeTeam.TypeName}}">
<label>{{typeTeam.TypeName}}</label>
</md-checkbox>
</span>
</div>
</div>
Теперь ng-repeat="typeTeam in $parent.TeamTypes"
работает так, как ожидалось, однако ng-hide="{{$parent.TeamTypes.length == undefined}}"
не работает. Это результат рендеринга:
<div class="block block-bordered ng-hide" ng-hide="false" aria-hidden="true">
<div class="block-content">
<div class="typeTeams">
<!-- ngRepeat: typeTeam in $parent.TeamTypes --><span ng-repeat="typeTeam in $parent.TeamTypes" class="ng-scope">
<md-checkbox aria-label="Team A" flex="" role="checkbox" class="flex"><div class="md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="md-icon"></div></div><div ng-transclude="" class="md-label">
<label class="ng-binding ng-scope">Team A</label>
</div></md-checkbox>
</span><!-- end ngRepeat: typeTeam in $parent.TeamTypes --><span ng-repeat="typeTeam in $parent.TeamTypes" class="ng-scope">
<md-checkbox aria-label="Under 21" flex="" role="checkbox" class="flex"><div class="md-container md-ink-ripple" md-ink-ripple="" md-ink-ripple-checkbox=""><div class="md-icon"></div></div><div ng-transclude="" class="md-label">
<label class="ng-binding ng-scope">Under 21</label>
</div></md-checkbox>
</span>
</div>
</div>
</div>
Как вы можете видеть, ng-repeat
работает, а атрибут ng-hide
- false, но класс ng-hide
по-прежнему находится в атрибуте class:
<div class="block block-bordered ng-hide" ng-hide="false" aria-hidden="true">
Я думал, что это ошибка асинхронного вызова apiService, поэтому я изменил метод $ scope.GetTeamTypes, который делает $ apply():
$scope.GetTeamTypes = function (organizationId) {
apiService.GetTeamTypes(organizationId).success(function (response) {
$scope.TeamTypes = response;
$scope.$apply();
});
};
но когда запускается $ scope.ClubOrganization_Change, я получаю эту ошибку:
angular.js:12416 Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.5/$rootScope/inprog?p0=%24digest
at angular.js:68
at beginPhase (angular.js:16235)
at Scope.$apply (angular.js:15976)
at userModalController.js:222
at angular.js:10215
at processQueue (angular.js:14634)
at angular.js:14650
at Scope.$eval (angular.js:15878)
at Scope.$digest (angular.js:15689)
at Scope.$apply (angular.js:15986)
Что я делаю не так?
ng-hide
/ng-show
не должно иметь {{}}
в своем выражении, вы можете упростить его ниже.
ng-hide="!$parent.TeamTypes.length"
Примечание. Используя
$parent
при обращении к переменной области видимости в HTML, не считайте хорошей практикой, используйтеDot rule
controllerAs
илиDot rule
при определении моделей.