先说使用场景,动态生成DOM元素并绑定事件,很是常见的一种场景,用jq实现效果:html
http://jsbin.com/gajizuyuju/edit?html,js,outputangularjs
var count=0; $("#test").on("click",function(event){ if(event.target.tagName.toLowerCase()=="input") return; count++; var html="<input type='text' class='newEle' value='"+count+"'/>"; $(this).html(html); $(".newEle").focus(); }); $("body").on("blur",".newEle",function(){ alert($(this).val()); })
若是用angularjs应该怎么实现呢?想固然的状况是这样的:api
var myApp = angular.module('myApp', []); myApp.controller('MainCtrl', ['$scope','$compile',function($scope) { $scope.count = 0; $scope.add = function() { if(event.target.tagName.toLowerCase()=="input")return; var target=$(event.target); $scope.count++; target.html("<input value='"+$scope.count+"' ng-blur='showValue()'>" ); } $scope.showValue=function(){ alert(event.target.value) } }])
理想很丰满,点击test的时候内容确实变成了input,可是input不能绑定任何ng事件。this
稍微修改下:http://jsbin.com/zujalapone/edit?html,js,outputspa
var myApp = angular.module('myApp', []); myApp.controller('MainCtrl', ['$scope','$compile',function($scope, $compile) { $scope.count = 0; $scope.add = function() { if(event.target.tagName.toLowerCase()=="input")return; var target=$(event.target); $scope.count++; target.html($compile("<input value='"+$scope.count+"' ng-blur='showValue()'>")($scope)); } $scope.showValue=function(){ alert(event.target.value) } }])
达到目的~code
这里用到了$compile服务,官方的解释是compile能够将一个HTML字符串或者DOM编译成模板,该模板可以与scope连接起来,也就是说直接插入一段html片断到页面中,虽然能插入进去,可是angular并无编译,因此任何ng事件指令绑定都是无效的,经过compile可以将html片断先编译后再插入。htm