AngularJS: Adding ng-click within element.append

6.1k views Asked by At

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 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':
                    element.append('<button class="btn btn-info" ng-click="selectProperties()" title="Assign this user"><span class="glyphicon glyphicon-user"></span>Assignment</button>');
                    break;
            }
        }
    };
});

So when i click on the button the selectproperties function should be called which is within the controller surrounding the element being appended. However it is not being called, and I know the function works correctly because if i put this button code straight into the html page it will work.

I have seen that using $compile is a way of getting around this, but when i use that it just causes my web page to freeze, and no errors come up in console.

I tried a couple other methods like adding a controller with the method into my directive -

    controller: function ($scope, $element) {
        $scope.selectProperties = function () {
            aler('test');
        }
    }

but this also didn't work. And I need to be able to use the function call to update an object in my main controller so I'm not sure I could do it that way.

Any ideas?

1

There are 1 answers

3
Kalhan.Toress On BEST ANSWER

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;
      }
    }
  };
});