Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Realized that to implement some front-end stuff in my project, it`s better to use a front-end framework, not just a pure JS. Choose AngularJS. Trying to implement and generated code and stuck with it...

I`ve got this html code generated and and added to the page content on some action,

function Content(i){
    var html =
        '<div ng-controller="Hello">'+
        '<button ng-click="click_btn()">Load Data!</button>'+
            '<div class="panel panel-default">'+
                '<div class="panel-heading">Items List</div>'+
                '<div class="col-sm-8">'+
                    '<ul>'+
                        '<li ng-repeat="element in content"> {{ element }} '</li>'+
                    '</ul>'+
                '</div>'+
            '</div>'+
        '</div>';

        var ul = document.getElementById('FullList');
        ul.insertAdjacentHTML('beforeend', html);
        AddElementAngular(contentString);
}

And the problem is that AngularJS <div ng-controller="Hello"> won't work here. can't get why, because code is successfully added to the page. I'm also tried to add click_btn() action and test the controller call on this action, but still doesn't work.

Here is the controller's code

angular.module('MainPage', [])
    .controller('Hello', function($scope, $http) {
        alert('jijij');
        $scope.click_btn = function() {
            alert('fsdfsdfsd');
        }

        $http.get('/api/getlist?groupID=2').
            success(function(data) {
                $scope.content = data;
            });
    });

The page is big to paste it here, but it starts with <html ng-app="MainPage">, so there shouldn't be a problem.

I'm inserted alerts to check, does the code is running. with the dynamic controller load - no. If i just insert controller's code directly in page content - everything works ok.

Controller inserted to the page dynamically only once. If the function Content(i){} call made again - previous controller's code will be removed.

EDIT:

Found a way to inject generated controller to the scope, from here: http://geevcookie.com/2014/01/compile-dynamic-html-with-angularjs/

function AddElementAngular(html){
    var $div = $(html);
    $(document.body).append($div);

    angular.element(document).injector().invoke(function($compile) {
        var scope = angular.element($div).scope();
        $compile($div)(scope);
    });
}

But still didn't work properly. Code in the controller is executed(

$http.get('/api/getlist?groupID=2').
    success(function(data) {
        alert(data);
        $scope.content = data;
    });

i get the [object][Object]... etc in alert. ), but the in-page <li ng-repeat="element in content"> {{ element }} </li> doesn`t work for unknown reason. I just getting '{{ element }}' in HTML content.

share|improve this question
1  
What are you trying to achieve by replacing a controller div ? I have a dirty solution but i'd really like to find the "clean" way to do what you want. Just keep it mind that 99% of the time, moving HTML from JS is a bad idea and could be do in a clean angular way. –  Okazari May 26 at 15:23
1  
if i can make this function function Content(i){} in Angular's way thats ok too. But i need to generate a html in JS/Angular controller. Too much to explain that i'm trying to achive for one case here. But this HTML generation should be done inside of .js/ angular controller. there will be much dynamic stuff added. 'What are you trying to achieve by replacing a controller div ?' - Sorry, maybe i described smth not clear enought, but i`m not replacing it, i'm generating it in js and putting in the main body. There is no is no controller exists in the body before it's generated. –  user1935987 May 26 at 16:21
1  
Actually this case looks worth than i though. I would have tell you to use the $compile service. If you modify the DOM adding directives from outside angular (and sometimes even inside) it need to interpret them. You can use the $compile service. But without any controller i'm not sure about the way to do that. Next question then, why can't you add this HTML directly into the HTML ? (I'm pretty sure you need some angular routing or things like that.) –  Okazari May 26 at 16:52
1  
because i can`t add it directly to html, if i can - i'll not create this question:) need to add controller dynamically. –  user1935987 May 26 at 17:49
1  
Keep in ming that i can't help you if you don't give more information. I'm not judging what you're doing, but i'm trying to understand why you want to do that to provide you a solution. I don't need to know what you think you probably need but what you want to achieve exactly. Next one : "Why" can't you add it directy to html ? –  Okazari May 26 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.