2016-11-19 50 views
0

我正在生成基於行大小的分頁按鈕。我的問題是,當我在我的AngularJS函數中創建按鈕並將其綁定到按鈕正常顯示的元素上時,但ng-click不起作用。如何用ng-click綁定AngularJS按鈕?

這裏是我的代碼:

var app = angular.module('myapp', []); 
var url = "<?php echo base_url(); ?>"+"index.php/"; 
var rowcount = <?php echo $this->session->userdata("count"); ?>; 


app.controller('myctrl', function($scope, $http, $sce) 
{ 
    $http.get(url+"exammanager/0/10").then(function(response) 
    { 
     $scope.myData = response.data.exams;   
     $scope.refresh();     
    }); 

    $scope.refresh = function() 
    { 
     $http.get(url+"exammanager/0/10").then(function(response) 
     {   
      $scope.myData = response.data.exams;  
      num = Math.ceil(rowcount/10); 
      counter = 0; 
      var buttons = ""; 
      for(i = 1;i <= num; i++) 
      { 
       if(i != num) 
        buttons += "&nbsp<button ng-click=\"pagescopeclick(\'"+url+"index.php/exammanager/"+counter+"/10\')\" >"+i+"</button> |"; 
       else 
        buttons += "&nbsp<button ng-click=\"pagescopeclick(\'"+url+"index.php/exammanager/"+counter+"/10\')\" >"+i+"</button> "; 
       counter = counter + 10;   
      } 
      $scope.count = $sce.trustAsHtml(buttons);     
     });  
    }//Refresh controller 

    $scope.pagescopeclick = function(val) 
    {alert(); 
     $http.get(val).then(function(response) 
     {   
      $scope.myData = response.data.exams;     
     });  
    }//pagescopeclick move to the next page without refresh 
}); 
+0

你必須注射到DOM之前編譯元素,你可以使用'$編譯'爲相同.. –

回答

0

潘卡是正確的,你需要使用$編譯。嘗試編輯你的刷新函數,就像這樣,你可以在for循環之後使用$ compile。 (不要忘記注入$編譯爲依賴)

$scope.refresh = function() { 

    $http.get(url+"exammanager/0/10").then(function(response) 
    {   
     $scope.myData = response.data.exams;  
     num = Math.ceil(rowcount/10); 
     counter = 0; 
     var buttons = ""; 
     for(i = 1;i <= num; i++) 
     { 
      if(i != num) 
       buttons += "&nbsp<button ng-click=\"pagescopeclick(\'"+url+"index.php/exammanager/"+counter+"/10\')\" >"+i+"</button> |"; 
      else 
       buttons += "&nbsp<button ng-click=\"pagescopeclick(\'"+url+"index.php/exammanager/"+counter+"/10\')\" >"+i+"</button> "; 
      counter = counter + 10;   
     } 
     $compile(buttons)(scope) 
     $scope.count = $sce.trustAsHtml(buttons);     
    });  

} 
+0

@PankajParkar它的工作,我把它替換爲你說的但ng單擊事件不是 – darrisb

0

我解決這個問題:

$scope.refresh = function() 
{ 
    $http.get(url+"exammanager/0/10").then(function(response) 
    {   
     $scope.myData = response.data.exams;  
     num = Math.ceil(rowcount/10); 
     counter = 0; 
     var buttons = ""; 
     for(i = 1;i <= num; i++) 
     { 
      if(i != num) 
       buttons += "<button ng-click='pagescopeclick(\""+url+"exammanager/"+counter+"/10\")' >"+i+"</button>"; 
      else 
       buttons += "<button ng-click='pagescopeclick(\""+url+"exammanager/"+counter+"/10\")' >"+i+"</button> "; 

      counter = counter + 10;   
     }    
     angular.element(document.getElementById('count')).html($compile(buttons)($scope));     
    });  
}