AngularJS: Adding ng-click within element.append

前端 未结 1 1639
野趣味
野趣味 2021-01-16 14:54

Within my directive I have the following code, which will be used to continually append to an html element.

//Establishes the type of question and therefore          


        
1条回答
  •  温柔的废话
    2021-01-16 15:21

    You should compile the html to bind the directives like ng-click to scope properties. Other vice angular directives will not bind to the scope properties.

    var strElm = '';
    var compiledHtml = $compile(strElm);
    element.append(compiledHtml);
    

    and don't remove $compile service from directive,

    and your code should be like,

    //Establishes the type of question and therefore what should be displayed
    app.directive('questionType', function($http, $compile) {
      return {
        restrict: 'A',
        link: function(scope, element, attr, model) {
    
          switch (scope.Question.inputType) {
            case 'checkbox':
              //element.append('');
              break;
            case 'text':
              var strElm = '';
              var compiledHtml = $compile(strElm);
              element.append(compiledHtml);
              break;
          }
        }
      };
    });

    0 讨论(0)
提交回复
热议问题