如何从指令

时间:2016-02-02 14:13:20

标签: angularjs angularjs-directive

我有角度控制器,它被分解成一个指令。

在该指令中,原始代码试图调用一个仍然驻留在父控制器中的函数,但声称它没有定义,所以我现在认为它是范围问题。

目前,该指令中的代码为

   setMessageState(false, true, "Problem obtaining statement");

其中setMessageState在主控制器中定义 - 如何启用此指令以伸出仍然调用该函数。

编辑: 根据要求,可以提供更多代码。这是我的指令.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;
    }

1 个答案:

答案 0 :(得分:0)

它不起作用,因为父控制器中的函数可能也会扭曲成函数。

您尝试做的与此类似。这也行不通。

(function (){
    function hello (){
        console.log("hello");
    }
})();


(function (){
        hello();
})()

关于隔离范围的整个想法不是在指令之外的任何东西上传递,除了通过参数传递到范围的东西。在您的情况下,transaccountNumber

所以你有几个选择。

  1. 您可以将功能转移到服务中并将服务注入指令控制器
  2. 将功能从父控制器传递到范围。我认为它是这样完成的:yourFn = '&'
  3. 或者您将外部控制器的功能分配给外部控制器的范围,并通过$scope.$parentScope.setMessageState在内部控制器中访问它。实际上这是最糟糕的解决方案,因为你根本不需要一个孤立的范围。
相关问题