2017-11-18 1 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 바인드 후 테이블을 반환

<div directive></div> 

을 사용하고 당신에 요청하고 있기 때문에

+0

그래, 아니, 정말로, 그것은 angularjs가 사용되기로되어있는 방법이 아닙니다. 그리고 당신은 비 동시성을 이해하지 못합니다. 템플릿은 완전히 정적 인 HTML 조각입니다. 템플릿 내부에서 ng-repeat 등을 사용하여 각도 표현을 사용하고 데이터에 바인딩하여 동적으로 만듭니다. 잠시 동안 지시문을 잊어 버리고 기본적인 컨트롤러와 뷰에 대해 배우십시오. –

답변

0

백엔드 (내 생각 엔)

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

지금 당장하고 있습니다. –

관련 문제