当尝试从 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
与您第二个代码中的功能不同。
如果您想从控制器外部调用方法,请查看 angular 中的事件。这将是存档的最佳方式。您还可以使用一个虚拟对象并将其双向绑定 (scope: { dummy: '=' }
),然后从其他代码部分调用您在指令中对该对象创建的方法。
看看这个演示这两种方法的 plnkr。
我正在尝试从其他 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
与您第二个代码中的功能不同。
如果您想从控制器外部调用方法,请查看 angular 中的事件。这将是存档的最佳方式。您还可以使用一个虚拟对象并将其双向绑定 (scope: { dummy: '=' }
),然后从其他代码部分调用您在指令中对该对象创建的方法。
看看这个演示这两种方法的 plnkr。