2017-11-18 3 views
0

こんにちは、私はAngularJsディレクティブからテーブルを返してバインドしようとしています。AngularJsディレクティブがテンプレートデータを返す前にレンダリングする

私の角度のコードは次のとおりです。

function directive($window, employeeDataServices) { 
    var directive = { 
     link: link, 
     restrict: 'EA', 
     renderOnFirstLoad: false, 
     template: myData() 
    }; 
    return directive; 
    function link(scope, element, attrs) { 
    } 
    function myData() { 
     angular.element(document).ready(function() { 
     employeeDataServices.getEmployees().then(function (response) { 
      var table = "<table>"; 
      table += "<tr>"; 
      table += "<th>Id</th>"; 
      table += "<th>Name</th>"; 
      table += "</tr>"; 
      angular.forEach(response, function (value, key) { 
      table += "<tr>"; 
      table += "<td>" + value.Id + "</td>"; 
      table += "<td>" + value.Name + "</td>"; 
      table += "</tr>"; 
      }); 
      table += "</table>"; 
      return table; 
     }); 
     }); 
    } 
    } 

とHTMLで私はあなたが要求を行っているのでAngularJsディレクティブは、HTMLバインド

+0

ええ、そうではありません、それはangularjsの使用方法ではありません。そして、あなたは非同期を理解していません。テンプレートは完全に静的なHTMLの部分です。テンプレート内でng-repeatなどを使用して角度式を使用し、データにバインドすることで動的にします。しばらくの間、ディレクティブを忘れて、基本的なコントローラーとビューについて学んでください。 –

答えて

0

後にテーブルを返す

<div directive></div> 

を使用していますバックエンド(私は思う)

employeeDataServices.getEmployees() 

ディレクティブにテンプレートプロパティを使用することはできません。あなたの機能を実装するために、ポストリンク機能を使用することができます

function directive($window, employeeDataServices) { 
var directive = { 
    restrict: 'EA', 
    renderOnFirstLoad: false, 
    compile: function() { 
     return { 
      post: function (scope, element, attrs) { 
       employeeDataServices.getEmployees().then(function (response) { 
        var table = "<table>"; 
        table += "<tr>"; 
        table += "<th>Id</th>"; 
        table += "<th>Name</th>"; 
        table += "</tr>"; 
        angular.forEach(response, function (value, key) { 
         table += "<tr>"; 
         table += "<td>" + value.Id + "</td>"; 
         table += "<td>" + value.Name + "</td>"; 
         table += "</tr>"; 
        }); 
        table += "</table>"; 
        element.append(table); 
       }); 
      } 
     } 
    } 
}; 
return directive; 
} 

確認する時間がありませんでしたが、要点を取得しました。

+0

あなたの返事をありがとう、それは私のために働く。 –

+0

あなたは答えを受け入れることができますか? –

+0

私は今それをやっていると確信しています。 –

関連する問題