2017-06-19 121 views
0

我有一个按钮菜单,角度动态创建。如果我按下一个按钮,它会显示一张包含一些数据的表格。当我打开应用程序时,如何按下默认显示的第一个按钮的数据?我的意思是,不按下按钮?然后当我按下另一个按钮时要更改的值。这可能吗?我的代码:默认角度自动按下按钮

<div ng-repeat="group in groups" class="btn-group"> 
    <button type="button" class="btn btn-primary" ng-click="switchGroup(group.id)">{{ '{{ group.code}}' }}</button> 
</div> 

<div ng-show="repartizations.length == 0"><h2> There is no schedule for this group.</h2></div> 

<table class="table table-responsive" ng-show="repartizations.length > 0"> 
    ... 
</table> 

角:

$http.get('/api/group/specialization/' + $state.params.valueSpecialization).then(function (success) { 
     $scope.groups = success.data; 
    } 
); 

$scope.switchGroup = function (groupId) { 
    $http.get(
     '/api/repartition/group/' + groupId 
    ).then(function (success) { 
     $scope.repartizations = success.data; 
    }); 
}; 

回答

0

调用该方法$scope.switchGroup()GET专业化API

$http.get('/api/group/specialization/' + $state.params.valueSpecialization).then(function (success) { 
    $scope.groups = success.data; 
    $scope.switchGroup($scope.groups[0].id) //Invoke the method 
}); 
+1

它的工作,谢谢你:)我会尽可能接受你的答案。 – IleNea

0

success回调方法撰写您点击按钮的代码里面ng-init()功能。 例如:

<div ng-init="myInitFunction()"> 
    // your code 
</div> 

现在,JS文件

$scope.myInitFunction = function(){ 
    $http.get(
     '/api/repartition/group/' + groupId 
    ).then(function (success) { 
     $scope.repartizations = success.data; 
    }); 
} 

所以每当页面刷新或重新加载,你可以看到在表中的数据。