Search code examples
javascriptangularjsangularjs-ng-repeatangular-ngmodelngsanitize

Why angular doen't get value of ng-models of inputs, html inserted by $sce.trustAsHtml?


I have this code on my controller:

angular.module('reporteadorApp')
    .controller('sidebarCtrl', ['$scope', '$http', '$cookies', '$sce', function ($scope, $http, $cookies, $sce) {
        $scope.codeControles = '<div class="row"><div class="col-sm-12">'
        $scope.codeControles += '<div class="form form-group"><label>' + asignarListaControles.nombreControl + '</label><select class="form form-control" ng-model="' + asignarListaControles.variable1 + '"></select></div>';
        $scope.codeControles += '</div></div>';
        $scope.codigoHTML = $sce.trustAsHtml($scope.codeControles);
}]);

Then in my HTML:

<div ng-bind-html="codigoHTML"></div>

I'm trying get the values of ng-models inserted on my view, but i can't, angular returns undefinided. Whats the problem?!. If i add an input manually and assign an ng -model, can get its value.

Also I can not add data to this select using ng-repeat

Thanks


Solution

  • Finally i resolve my problem with this:

    var htmlToInsert = $compile('<div>' + $scope.codeControles + '</div>')($scope);
    $('#yourDivName').html(htmlToInsert);
    

    Then in my HTML:

    <div id="yourDivName"></div>
    

    ;)