我在Laravel的Angular遇到了麻烦。我试图通过单击按钮来更改变量。
为此,我写了以下代码:
<div class="user-team-navigation col-sm-12" id="user-team-navigation" >
<ul>
<div ng-repeat="user_team in user_teams" ng-if="user_team.user_id == {{ $user_id }}">
<li><button ng-click="$parent.current_user_team = user_team.id">@{{ user_team.name }} @{{ $parent.current_user_team }}</button></li>
</div>
</ul>
</div>
我的控制器如下所示:
angular.module('wtmApp').controller('rideruserteamList', function($scope, $http) {
$scope.order_by = 'id';
$scope.order_way = 'asc';
$http.get('/API/userteamriders-list.php').then(
function(response) {
$scope.rider_userteams = response.data.rider_userteams;
}
);
$scope.current_user_team = 1;
$scope.amount_of_riders = 0;
});
稍后在代码中我调用变量:$ parent.current_user_team 但它不会更新。它应该切换到用户的第2组,但它仍然通过编写以下代码来显示来自第1组的车手:
<div class="riders col-sm-3" ng-repeat="rider_userteam in rider_userteams" ng-if="rider_userteam.user_id == {{ $user_id }} && rider_userteam.user_team_id == $parent.current_user_team">
正如您之前所见,我也在ng-click按钮上打印变量 显示的变量确实会更改,但仅限于特定按钮。因此,变量不会在ng-click范围之外变化。您是否有任何建议如何在ng-click 之外制作我的$ parent.current_user_team变量。
我期待您的回复!如果有必要提供更多信息,请与我们联系。
拉特格
答案 0 :(得分:0)
不使用$ parent / $ root / normal变量,只需使用object即可。请找到代码段。
<div class="user-team-navigation col-sm-12" id="user-team-navigation" >
<ul>
<div ng-repeat="user_team in user_teams" ng-if="user_team.user_id == {{ $user_id }}">
<li><button ng-click="current_user_team.id = user_team.id">@{{ user_team.name }} @{{ $parent.current_user_team }}</button></li>
</div>
</ul>
</div>
控制器中的变量相同,
angular.module('wtmApp').controller('rideruserteamList', function($scope, $http){
$scope.order_by = 'id';
$scope.order_way = 'asc';
$http.get('/API/userteamriders-list.php').then(
function(response) {
$scope.rider_userteams = response.data.rider_userteams;
}
);
$scope.current_user_team = {
id:1
};
$scope.amount_of_riders = 0;
});
HTML,
<div class="riders col-sm-3" ng-repeat="rider_userteam in rider_userteams" ng-if="rider_userteam.user_id == {{ $user_id }} && rider_userteam.user_team_id == current_user_team.id">
请参阅angularjs docs中的scope hierarchy