簡體   English   中英

當嘗試從Controller調用函數時,它沒有響應

[英]When try to call function from Controller it doesn't respond

我正在嘗試從其他javascript文件調用我的jointController中的函數。

var app1 = angular.module('jointApp',[]);
var createStateBox = function(label,color){

var state = new uml.State({
    position: {x: 0, y: 0},
    size: {width: 200, height: 100},
    name: "<<"+label+">>",
    events: [label+" box"],
    attrs:{rect:{fill:color},path:{"stroke-width":0}}

});
app1.controller('jointController', function($scope) {
    $scope.setDecision(state);
    alert("This is reached");
});
    paper.model.addCell(state);
}

這是jointMod.js中的代碼,其中包含jointController

var app = angular.module('jointApp', []);

function JointController($scope, $http, $filter) {

    $scope.list = [];

    $scope.newMsg = 'hello';
    $scope.newDecision;

    $scope.setMsg = function(msg) {
        $scope.newMsg = msg;
    }

    $scope.sendPost = function() {
        var data = $.param({
        json: JSON.stringify({
            msg: $scope.newMsg
            })
         });

    $scope.setDecision = function(decision){
        $scope.newDecision = decision;
        alert("one two one two")
        console.log(decision);
    }

    $http({
        method: 'POST',
        url: 'http://213.65.171.121:3000/decision',
        data: $scope.newMsg,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformRequest: function(obj) {
            var str = [];
            str.push(encodeURIComponent("action") + "=" + encodeURIComponent(obj));
            return str.join("&");
        }
    }).success(function(data, status, header, config) {
                $scope.list.push(status);
        }).error(function(data, status, header, config) {
                $scope.list.push(status);
        });
    };
};

我在那里有警報和控制台登錄信息,以確保是否可以訪問它們,但沒有響應。

您提供的代碼將無法正常運行。 如果同時使用,則不要。

這兩個代碼都引入了一個新的模塊jointApp ,實際上只有第一個模塊定義了一個控制器。 該控制器的$scope與第二個代碼中的功能不同。

如果要從控制器外部調用方法,請查看角度事件。 那將是最好的存檔方法。 您還可以使用一個虛擬對象並將其雙向綁定( scope: { dummy: '=' } ),然后從其他代碼部分中在指令中調用在該對象上創建的方法。

看一下展示兩種方法的plnkr。

http://plnkr.co/edit/YdcB10UpXGqKAxYzXISL?p=preview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM