Javascript控制器函数未定义

时间:2017-07-25 17:03:32

标签: javascript angularjs angularjs-controller

我最近发了一篇文章,但不容易阅读...... 我希望通过这个你能理解我的问题:

基本上我有2个控制器,在第一个中,我创建了一个div,它具有一个'onclick'属性,可以调用另一个控制器上的函数(newController)。错误是:未定义activateSmartcase。

我很绝望lol ..感谢您的帮助。

[编辑解决方案]

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

手动获取http依赖。

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

})();

0 个答案:

没有答案