2014-08-28 42 views
1

如何在angularjs上动态添加指令?Uncaught TypeError:对象不是函数 - 在angularjs上动态添加指令

我已经搜索了很长时间,但很难找到帮助。我错在哪里?我真的不知道该怎么做。我写了下面的所有细节。我试过了,没有成功。

我试过以下,但没有成功:

.directive('addDeleteTemplate', function() { 
     return { 
      link: function (scope, element, $compile) { 
       element.on('click', function() { 
        var jQuery = element.parents('tr'); 
        var find = jQuery.find('td[class=delete]'); 
        var index = $compile('<div delete-news></div>')(scope);// error : Uncaught TypeError: object is not a function 
        angular.element(find).append(index); 
       }); 
      } 
     }; 
    }) 



.directive("deleteNews", [ 
     'NewsApkService', function (NewsApkService) { 
      return { 
       restrict: 'A', 
       templateUrl: getBaseUrl() + 'Templates/News/Delete', 
       link: function (scope, element, attrs) { 
        //element.find('button').bind('click', function (e) { 
        //  
        //}); 
       }, 
       controller: function ($scope, $element, $compile) { 

       } 
       } 
      } 
     } 
    ]) 



<a add-delete-template> 

</a> 

这是什么错误:

Uncaught TypeError: object is not a function

回答

2

更改您的第一个指令,这个

.directive('addDeleteTemplate', function($compile) { 
    return { 
     link: function (scope, element, attrs) { 
      element.on('click', function() { 
       var jQuery = element.parents('tr'); 
       var find = jQuery.find('td[class=delete]'); 
       var index = $compile('<div delete-news></div>')(scope);// error : Uncaught TypeError: object is not a function 
       angular.element(find).append(index); 
      }); 
     } 
    }; 
}) 
相关问题