2016-06-08 48 views
0

controller.js

$scope.getElectInfo=function(){ 
     var selectedVal = $("#countriesList option:selected").val(); 
     if (selectedVal != 0) { 
     $.get(APP_URL+ "/political/ElectionType/GetElectionTypesByCountry?countryId=" 
           + selectedVal, 
         function(data, status) { 
         $scope.data1=data; 
         console.log($scope.data1); 
         }); 
        $scope.Structure = true;  
    }else 
    { 
    $scope.Structure = false; 
    } 
     } 

HTML

<select id="countriesList" ng-change="getElectInfo()" ng-model="getElectionInfo"> 
     </select> 
     <div ng-show="Structure" class="content"> 

<figure class="org-chart cf"> 
<ul class="administration"> 
    <li>     
    <ul class="director"> 
     <li> 
     <ul class="subdirector"> 
     </ul> 
     <ul class="departments cf">        
      <li><a ><span ng-repeat="cname in country">{{cname.countryName}}</span></a></li> 

      <li class="department dep-b" ng-repeat="types in data1"> 
      <a ><span>{{types.name}}</span></a> 
      </li> 

在上面controller.js我得到全國大選明智類型的數據,在html頁面,當我第一次選擇國家$ scope.data1不更新在視圖中,選擇第二次更新可以任何一個幫助我什麼是問題!

+1

刪除行以獲取選定值並將其替換爲您的變量getElectionInfo。 確保您的條件「!= 0」爲真,並檢查getElectionInfo的值 – Silvinus

+0

沒有運氣,同樣的問題 – arjun

+0

您檢查了getElectionInfo的值嗎?你確定請求正在執行並返回一個數組嗎?你能第一次提供請求結果嗎? – Silvinus

回答

1

終於我明白了,

需要放置$ scope。$ apply();功能(數據,狀態){}

+0

哼哼......我明白了。我可以向你推薦一些更新以避免調用$ scope。$ apply()? 你必須調用它,因爲你使用jQuery的http查詢,你不在角度摘要週期。注入$ http角度服務來完成你的http請求。 Angular會自動調用他的摘要階段來更新視圖。 – Silvinus

+0

我怎麼能達到這個請幫助我! – arjun

+0

在你的控制器中注入$ http,如下所示: app.controller('controllerName',['$ http',function($ http){...}]) 並用它來做你的請求: $ http .get(APP_URL +「/ political/ElectionType/GetElectionTypesByCountry?countryId =」+ selectedVal).then(function sucess(response){$ scope.data1 = response.data;},function error(error){/ * handle error */}) – Silvinus