Javascript controller -function is not defined

假装没事ソ 提交于 2020-01-17 06:40:11

问题


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

})();

来源:https://stackoverflow.com/questions/45309588/javascript-controller-function-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!