ng-click无法使用动态生成的HTML

Kir*_*mar 49 html jquery angularjs

HTML

<table data-ng-table="tableParams" class="table table-bordered table-hover " style="border-collapse:collapse" data-ng-init="host.editSave = false" >
    <tr id="newTransaction">
    </tr>
    <tr data-ng-repeat="host in hosts|filter:search:strict" >
       <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostCd}}</td>
       <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostName}}</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('#newTransaction').append(
 '<td contenteditable><input type="text" class="editBox" value=""/></td>'+ 
 '<td contenteditable><input type="text" class="editBox" value=""/></td>'+
 '<td>'+
    '<span>'+
        '<button id="createHost" class="btn btn-mini btn-success" data-ng-click="create()"><b>Create</b></button>'+
    '</span>'+
 '</td>'
);
Run Code Online (Sandbox Code Playgroud)

角度脚本

$scope.create = function() {
       alert("Hi");
    };
Run Code Online (Sandbox Code Playgroud)

这里AngularJS的控制器部分中调用的函数没有从ng-click事件获得触发器.Html成功附加,但ng-click无法正常工作.告诉我解决方案,让它工作

Aru*_*hny 47

不是一个完美的修复,仍然! - 只是为了展示如何进行动态编译

app.controller('AppController', function ($scope, $compile) {

    var $el = $('<td contenteditable><input type="text" class="editBox" value=""/></td>' +
        '<td contenteditable><input type="text" class="editBox" value=""/></td>' +
        '<td>' +
        '<span>' +
        '<button id="createHost" class="btn btn-mini btn-success" data-ng-click="create()"><b>Create</b></button>' +
        '</span>' +
        '</td>').appendTo('#newTransaction');
    $compile($el)($scope);

    $scope.create = function(){
        console.log('clicked')
    }
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴

不要使用控制器进行dom操作 - 它必须在指令的帮助下完成

  • @KiranKumar可用性,代码清晰度,维护.控制器应该关注业务逻辑,而不是接口操作. (4认同)

Max*_*tin 36

为了使用ng-click,我们需要使用服务来编译这个源代码$compile.Angular应该知道新生成的HTML,因此应该将此HTML包含在摘要周期中以便触发ng-click和其他事件.

看到 Fiddle

创建"compilator":

.directive( 'compileData', function ( $compile ) {
  return {
    scope: true,
    link: function ( scope, element, attrs ) {

      var elmnt;

      attrs.$observe( 'template', function ( myTemplate ) {
        if ( angular.isDefined( myTemplate ) ) {
          // compile the provided template against the current scope
          elmnt = $compile( myTemplate )( scope );

            element.html(""); // dummy "clear"

          element.append( elmnt );
        }
      });
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

之后,创建factory模拟你追加的假人:

.factory( 'tempService', function () {
  return function () { 
    return '<td contenteditable><input type="text" class="editBox" value=""/></td>'+ 
            '<td contenteditable><input type="text" class="editBox" value=""/></td>'+
             '<td>'+
                '<span>'+
         '<button id="createHost" class="btn btn-mini btn-success" data-ng-click="create()"><b>Create</b></button>'+
              '</span>'+
            '</td>';
  };
});
Run Code Online (Sandbox Code Playgroud)

最后称之为:

<div compile-data template="{{mainPage}}"></div> 
Run Code Online (Sandbox Code Playgroud)

在控制器中:

$scope.newTransaction= tempService();
Run Code Online (Sandbox Code Playgroud)

对于你的例子应该是这样的:

<table data-ng-table="tableParams" class="table table-bordered table-hover " style="border-collapse:collapse" data-ng-init="host.editSave = false" >
    <tr compile-data template="{{newTransaction}}">
    </tr>
    <tr data-ng-repeat="host in hosts|filter:search:strict" >
       <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostCd}}</td>
       <td class="hostTableCols" data-ng-hide="host.editSave">{{host.hostName}}</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

BTW,现在你可以在你的代码上使用相同的指令并编译任何动态HTML.


por*_*ras 20

您可以在angular.element(this).scope()不使用ng-click的情况下使用

并改变

'<button id="createHost" class="btn btn-mini btn-success" data-ng-click="create()"><b>Create</b></button>'

'<button id="createHost" class="btn btn-mini btn-success" onclick="angular.element(this).scope().create()"><b>Create</b></button>' 很好