AngularJS: Adding ng-click within element.append

前端 未结 1 1627
野趣味
野趣味 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 = '<button class="btn btn-info" ng-click="selectProperties()" title="Assign this user"><span class="glyphicon glyphicon-user"></span>Assignment</button>';
    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('<input type="checkbox" ng-model="Question.checked"/><button ng-if="input.checked" >X</button>');
              break;
            case 'text':
              var strElm = '<button class="btn btn-info" ng-click="selectProperties()" title="Assign this user"><span class="glyphicon glyphicon-user"></span>Assignment</button>';
              var compiledHtml = $compile(strElm);
              element.append(compiledHtml);
              break;
          }
        }
      };
    });

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