2017-12-20 2 views
0

함수 안에 html 템플릿을 생성하고 버튼을 추가했습니다. 내가 그렇게하고있는 이유는 d3.js v3를 사용하여 생성 된 scatterplot에서 데이터 노드를 클릭 할 때이 템플릿을 팝업으로 표시하기 때문입니다. 데이터 포인트를 클릭하면 데이터 포인트에 대한 정보와 추가 텍스트를 추가 할 수있는 텍스트 영역이 입력 상자에 팝업됩니다. 그러나 ng-click 및 ng-model과 같은 각도 지시문은 해당 버튼에 사용하면 실행되지 않습니다. 나는 뭔가를 놓치고 있는가, 아니면 내 방식이 잘못 되었습니까?컨트롤러 안의 html 템플릿 안에 ng-click이 추가되면 작동하지 않습니다.

vm.addAnnotateBox = function(event) { 
    var annotateText = "<div class='annotateBox'>" + "<h5 style='font-size: 22px;color:#fff; white-space:normal;'>"; 
    annotateText = annotateText + "<h6 style='color:#dae1e1'>"; 
    annotateText = annotateText + event.label + "</h5><small style='color:#dae1e1'>"; // jscs:disable maximumLineLength 
    annotateText = annotateText + event.time + "</small>"; 
    annotateText = annotateText + "<div class='row'>"; 
    annotateText = annotateText + "<textarea rows='3' cols='50' maxlength='100' style='color:black' ng-model='vm.annotateText' autofocus></textarea>"; 
    annotateText = annotateText + "</div>"; 
    annotateText = annotateText + "<button type='button' class='btn btn-primary pull-right' ng-click='d3.select(this.parentNode).remove()'>Done</button></div>"; 
    return annotateText; 
}; 

onclick은 정상적으로 작동하지만 클릭하지 않습니다. 컨트롤러 내부에 html 템플릿을 만드는 것이 잘못 되었습니까? 나는 데이터 포인트의 클릭 속성에 위의 함수를 호출 오전 :

eventGroup.select("circle") 
      .attr("class", "dot") 
      .attr("r", 4) 
      .attr("cx", 10) 
      .attr("cy", 10) 
      .attr("fill", function(d) { return d.evtColor ? d.evtColor : "#229ae5"; }) 
      .attr("stroke", function(d) { return d.evtColor ? d.evtColor : "#229ae5"; }) 
      .attr("stroke-width", 2) 
      .on("click", vm.addAnnotateBox()); 

템플릿은 현재 클릭에 나타납니다하지만 난 "완료를 클릭에 NG-모델을 사용하여 텍스트 영역에 입력 한 텍스트를 저장할 수 없습니다 오전 "버튼을 클릭해도 원하는 기능이 실행되지 않습니다. enter image description here

+1

Angularjs 지시문은 페이지로드에 바인딩됩니다. 동적으로 추가 된 요소는 지시문을 컨트롤러 나 모듈에 바인딩하지 않습니다. '$ compile' 서비스를 보거나 DOM에 수동으로 추가하는 대신 사용자를위한 요소를 생성 할 사용자 지정 지시문/구성 요소를 작성하십시오. – mhodges

+2

[AngularJS에 동적으로 지시문을 추가하십시오.] (https://stackoverflow.com/questions/) 15279244/dynamically-add-directive-in-anglesjs) – mhodges

답변

1

이러한 상황을 처리하는 올바른 방법은 아닙니다.

ng-show/ng-hide를 사용하면 상황에 따라 표시하거나 숨길 수 있습니다.

<div> 
<div> 
<label>First Name</label> 
<input type='input' ng-model='vm.FirstName' name=Fname /> 
</div> 
<div> 
label>Last Name</label> 
<input type='input' ng-model='vm.LastName' name=Lname /> 
<div> 
</div> 

<div class='annotateBox' ng-show="vm.showpannel"> 
<h5 style='font-size: 22px;color:#fff; white-space:normal;'> 
<h6 style='color:#dae1e1'> 
</h5><small style='color:#dae1e1'> 
</small> 
<div class='row'> 
<textarea rows='3' cols='50' maxlength='100' style='color:black' ng-model='vm.annotateText' autofocus></textarea> 
</div> 
<button type='button' class='btn btn-primary pull-right' ng-click='vm.remove()'>Done</button> 
</div> 
+0

나는 이것에 동의하며 이것은 나의 첫 번째 해결책이었다. 하지만 차트의 각 데이터 포인트를 클릭 할 때이 상자를 추가하고 싶습니다. 따라서 각 상자는 해당 데이터 요소로 제한됩니다. 이 솔루션이 이러한 행동을 처리합니까? – user2128

+0

그것은 또한 당신의 디자인에 달려 있어야하며 각 포인트에 고유 한 ID/이름을 부여해야합니다. 그래서 당신은 독특하게 숨기거나 보여줄 수 있습니다. –

관련 문제