0

I posted a post recently but it wasn't easy to read... I hope that with this one you could understand my problem:

Basicly i have 2 controllers, and in the first one, I create dinamically a div that has an 'onclick' attribute that calls a function that is on the other controller (newController). The error is: activateSmartcase is not defined.

I'm desperate lol.. thanks for help.

[EDIT SOLUTION]

var myInjector = angular.injector(["ng"]);
var $http = myInjector.get("$http");
function funcb($http){
}

manually getting the http dependency.

app.controller('groupCtrl', ['$scope','$http', '$resource',function($scope,$http,$resource) {


    $scope.smartcase=function(){

        $http.get('http://localhost:8080/userapi/getSmartcasesFromType/' + elements[0].alt)
        .then(function(result) {
            $scope.smartcases = result.data;
        });



        var div = document.createElement("div");
        div.setAttribute('class', "container");
        div.setAttribute('id',elements[0].alt);
        div.setAttribute('ng-controller',"newController");  
        div.setAttribute('title',elements[0].id);
        groupid=elements[0].id;
        div.setAttribute('alt',"smartcase");
        div.setAttribute('onclick', 'activateSmartcase()'); //HERE
        var div2= document.createElement("div2");
        div2.setAttribute('class',"list-group");

        div.appendChild(div2);

        var a = document.createElement("a");
        a.setAttribute('href',"#");
        a.setAttribute('class',"list-group-item active");

        div2.appendChild(a);
        for(var i=0; i<$scope.smartcases.length; i++) {

            var h = document.createElement("h4");
            h.setAttribute('class',"list-group-item-heading");              
            h.innerHTML=$scope.smartcases[i].type;
            var p=document.createElement("p");
            p.setAttribute('class', "list-group-item-text");
            p.innerHTML=$scope.smartcases[i].description;
            a.appendChild(h);
            a.appendChild(p);
        }
        document.body.appendChild(div);
    }

}]);
(function (){

    'use strict';

    angular
    .module('myAppGroup')
    .controller('newController', Controller);

    function Controller($scope,$http,$resource){
        const vm = this;
        vm.activateSmartcase = function(){
            //console.log(deviceId);
            var operations=[];
            $http.get('http://localhost:8080/userapi/listOperations/' + groupid)
            .then(function(result) {
                operations = result.data;
            });
            console.log(operations);

            var div = document.createElement("div");
            div.setAttribute('class', "dropdown");
            var button=document.createElement("button");
            button.setAttribute('class', "btn btn-default dropdown-toggle");
            button.setAttribute('type',"button");
            button.setAttribute('id', "dropdownMenu1");
            button.setAttribute('data-toggle',"dropdown");
            button.setAttribute('aria-haspopup',"true");
            button.setAttribute('aria-expanded',"true");
            var span=document.createElement("span");
            span.setAttribute('class',"caret");
            button.appendChild(span);
            div.appendChild(button);

            var ul=document.createElement("ul");
            ul.setAttribute('class',"dropdown-menu");
            ul.setAttribute('aria-labelledby',"dropdownMenu1");

            div.appendChild(ul);
        }
    }

})();
4
  • Or jquery or html? Commented Jul 25, 2017 at 17:05
  • In AngularJS, controllers shouldn't manipulate DOM. DOM manipulation should only be done in directives. In addition AngularJS directives added by elem.setAttribute will not be integrated with the AngularJS framework. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Commented Jul 25, 2017 at 19:35
  • The onclick attribute only executes functions on the global context. To execute functions on $scope or the this context of a controller, use a ng-click directive that is properly integrated with the AngularJS framework. Commented Jul 25, 2017 at 19:43
  • Ive already tried To set ng-click instead of onclick... bue it does nothing, it Looks like it is not recognized/triggered Commented Jul 26, 2017 at 0:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.