ng-include not working with script type=“text/ng-template”

前端 未结 3 1376
长发绾君心
长发绾君心 2020-12-30 06:55

Here is my Plunker:

http://plnkr.co/edit/oIei6gAU1Bxpo8VUIswt

When the button is clicked, the following should be inserted before the \"Hello World!\" span:<

3条回答
  •  无人及你
    2020-12-30 07:14

    The quick answer might have been:


    Probably you just forgot the single quotes to reference the template.

    The long answer:

    It is not advised to access the DOM inside a controller - you will get in trouble as the code will be flooded with $scope.$apply() calls. Think about implementing this feature with a directive. I tried to create a starting point from your code here

    http://plnkr.co/UWUCqWuB9d1dn6Zwy3J3

    var app = angular.module('plunker', ['ngAnimate']);
    
    app.directive('greeting', function($compile){
      return {
        restrict: 'E',
        scope: {
          name: '='
        },
        template: '
    '+ ' Hello {{name}}!'+ ' '+ '
    ', link: function(scope, element, attrs) { scope.insert = function() { var container = angular.element('
    '); element.before($compile(container)(scope)); } } } }) app.controller('MainCtrl', function($scope) { $scope.name = 'World'; });
    
    

    The template elements are inserted before the Hello World! textnode everytime the button is clicked.

    Side note You dont even need the scope{ name: '='} as the directive will inherit its surrounding scope, but its the cleaner way to pass (actually bind) controller variables to a directive explicitly.

提交回复
热议问题