2017-06-19 139 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; 
    }); 
} 

所以每當頁面刷新或重新加載,你可以看到在表中的數據。