나는 많은 대답을 읽었지만 문제를 해결할 수 없었습니다.
나는 다음 과 같은 요소 Javascript function
를 만들었습니다.HTML
document.createElement(...)
이것은 그 기능의 일부입니다.
var div=document.getElementById(params.wrapper);
div.setAttribute("ng-controller","ProjectController");
var button = document.createElement('button');
button.setAttribute('id','btn-view-users-' + params.id);
button.className='button-manage-user';
button.setAttribute('ng-click','hello()');
div.appendChild(button);
이 함수는 응용 프로그램 시작시 호출되고 많은 행을 생성하므로 페이지가로드 될 때 함수에 의해 생성 된 HTML은 다음과 같습니다. ( 이 샘플 코드는 이해하기 쉽게 단순화되었습니다. )
<div id="wrapper" class="wrapper ng-scope" ng-controller="ProjectController">
<button id="btn-view-users-310" class="button-manage-user" ng-click="hello()">Manage Users</button>
<button id="btn-view-users-311" class="button-manage-user" ng-click="hello()">Manage Users</button>
<button id="btn-view-users-312" class="button-manage-user" ng-click="hello()">Manage Users</button>
<button id="btn-view-users-313" class="button-manage-user" ng-click="hello()">Manage Users</button>
</div>
Angular 컨트롤러 :
app.controller('ProjectController',
['$scope', '$http',
function($scope, $http) {
$scope.hello = function(){
console.log("hello")
}
}
]
)
모든 요소가 생성 될 때 함수의 끝에서 다음으로 컴파일합니다.
var content = $(".wrapper");
angular.element(document).injector().invoke(function($compile) {
var scope = angular.element(content).scope();
$compile(content)(scope);
console.log("#compile")//log appears in the console 10 times if 10 elements are created.
});
하지만 버튼을 클릭하면 "hello"가 여러 번 실행됩니다!
만약 내가 10 개 요소 페이지를하고 클릭 첫 번째 요소는 불을하다 10 회 내가 클릭하면, 두 번째 는 해고 요소 9 번 등등과 ..
ng-controller
선언을 버튼 내부 button.setAttribute("ng-controller","ProjectController");
또는 <body>
태그 내부 로 이동하려고 시도 했지만 여전히 동일한 동작을 얻습니다.
도와 주셔서 감사합니다
controller
div를 컴파일하고 있기 때문에 div를 컴파일하고 컴파일 ng-controller="ProjectController"
지시문 을 수행 ProjectController
하고 div를 컴파일하는 횟수만큼 인스턴스를 등록합니다 .
$compile(content.contents())(scope);
전체 컨트롤러 요소를 컴파일하는 대신 수행해야합니다 .
그러나 compile
요소에는 루트 요소가 하나만 있어야합니다. 컴파일하려는 요소에는 정확히 하나의 루트 요소가 있어야하기 때문에 그렇게 할 수 없습니다. 기타는 해당 루트의 하위 요소 여야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다