У меня есть угловой контроллер, и его разделили на директиву.Как вызвать функцию в родительском контроллере из директивы
Внутри этой директивы исходный код пытается вызвать функцию, которая все еще находится в родительском контроллере, но она заявляет, что она не определена, поэтому я предполагаю, что теперь проблема с областью видимости.
В настоящее время код в директиве читает
setMessageState(false, true, "Problem obtaining statement");
где setMessageState определяется в основном контроллере - как я могу включить эту директиву, чтобы достучаться до сих пор называют эту функцию.
EDIT: В соответствии с запросом еще код. это мой directive.js
(function() {
'use strict';
//Define the Controller
angular
.module("tpAccStatus")
.directive("tpAccStatusTransactionRow", tpAccStatusTransactionRow);
function tpAccStatusTransactionRow() {
return {
scope: {
trans: "=tpAccStatusTransactionRow",
accountNumber: "="
},
restrict: "A",
replace: true,
templateUrl: "tpAccStatusTransactionRow.directive.html",
bindToController: true,
controller: "tpAccStatusTransactionRowCtrl",
controllerAs: "transCtrl"
};
}
})();
с контроллером, как:
(function() {
'use strict';
//Define the Controller
angular
.module("tpAccStatus")
.controller("tpAccStatusTransactionRowCtrl", tpAccStatusTransactionRowCtrl);
//Inject any dependencies.
//We don't need scope injecting as we are using the Controller as Syntax
tpAccStatusTransactionRowCtrl.$inject = ["$scope", "tpAccStatusService", "nsgLogger"];
//Implement the Controller
function tpAccStatusTransactionRowCtrl($scope, tpAccStatusService, logger) {
//////////////////////// Public Interface //////////////////////////////////////////////
//Ensure there are no issues with the transient nature of the this pointer in Javascript
var vm = this;
vm.busy = false;
vm.requestStatement = requestStatement;
vm.documentLocation = "";
////////////////////////////////////////////////////////////////////////////////////////
//Activate the controller to populate the view model with any set up data
activate();
///////////////////////////////////////////////////////////////////////////////////////
//Retrieve the Administration top Level View Model from the information from the server
function activate() {
}
// get document
function requestStatement() {
if (vm.documentLocation != "") {
window.location = vm.documentLocation;
}
else {
if ((vm.trans.DocumentNumber != null || vm.trans.DocumentNumber != 'none')) {
vm.busy = true;
//setBusyState(true);
return tpAccStatusService
.requestStatement(null, null, vm.accountNumber, vm.trans.DocumentNumber)
.then(requestStatementCompleted, requestStatementErrored);
} else {
logger.error("No Accounts Selected", "Analysis Statement");
}
}
}
function requestStatementErrored(error) {
vm.busy = false;
setMessageState(false, true, "Problem obtaining statement");
}
function requestStatementCompleted(guid) {
vm.busy = false;
vm.documentLocation = "/download/" + guid;
window.location = vm.documentLocation;
}
}
})();
вы можете увидеть ссылку на функцию setMessageState(), которая проводится в родительском контроллере, как:
function setMessageState(showSuccess, showError, message) {
vm.showSuccessMessage = showSuccess;
vm.showErrorMessage = showError;
vm.message = message;
}
Дополнительный код, пожалуйста. – isherwood
Возможный дубликат [AngularJS: вызов функции контроллера из директивы без изолированной области действия] (http://stackoverflow.com/questions/17583004/angularjs-call-a-controller-function-from-a-directive-without-isolated -scope) – isherwood
HI - Я смотрел на это ранее, но это только вызов функции, тогда как im также устанавливает значение, поэтому я не думаю, что это решение соответствовало моим обстоятельствам, когда у меня есть функция, которая принимает 3 параметра в основном контроллере - мне нужно вызовите это и передайте требуемые параметры из директивы – user3779703