簡體   English   中英

我可以將$ scope變量從一個函數傳遞到控制器內的另一個函數嗎? AngularJS

[英]Can I pass a $scope variable from one function to another function inside the controller? AngularJS

是否可以將$ scope變量傳遞給控制器​​內部的另一個函數?

第一個功能是包含ID的POST請求。

第二個函數是ng-click函數,需要$ scope.id變量。

有辦法通過它們嗎? 也許作為全局變量,或者我可以在第二個函數的參數列表中寫入$ scope.id。

第二種方法是使用ng-click =“ getResult({{id}},r.id)”在視圖中傳遞參數

這是我的控制器:

    .controller('HomeController',
['$scope', '$rootScope', 'SendJmeterFile', 'NotificationPollService', 'GetResultFile',
function ($scope, $rootScope, SendJmeterFile , NotificationPollService, GetResultFile) {

    $scope.upload = function() {

        $scope.dataTable = false;
        $scope.id = 0;
        $scope.results = "";
        var customArtifacts = "";
        var testDataBase = "";

        if($scope.jmeterFile.customArtifact == undefined){
            customArtifacts = null;
        } else {customArtifacts = $scope.jmeterFile.customArtifact.base64}

        if($scope.jmeterFile.testDataBase == undefined){
            testDataBase = null;
        } else {testDataBase = $scope.jmeterFile.testDataBase.base64}

        SendJmeterFile.upload($scope.jmeterFile.jmeter.base64, customArtifacts, $scope.jmeterFile.customProperties, $scope.jmeterFile.instanceConfiguration, $scope.jmeterFile.instances, $scope.jmeterFile.providerID, testDataBase)
            .then(function(data) {
                alert("Daten erfolgreich verschickt!");
                console.log(data);
                console.log(data.data.id);
                $scope.dataTable = false;
                $scope.id = data.data.id;
                var poller = new NotificationPollService($scope.id);
                poller.promise.then(onSuccess, onError, onNotify);

                function onSuccess(data) {
                    // data.status == "DONE"
                    console.log("done controller" + data);
                    $scope.dataTable = true;
                    $scope.results = data.data.results;
                };

                function onError(data) {
                // data.status == "ERROR"
                    console.log(data);
                    console.log("error controller" + data);
                    $scope.dataTable = false;
                };

                function onNotify(data) {
                    console.log(data);
                // data.status == "TEST" || data.status == "SETUP"
                    console.log("test/setup controller" + data);
                    $scope.dataTable = false;
                };

            });
            }, function(data) {
                alert("Fehler!");
                console.log("else controller" + data);
                $scope.dataTable = false;
            };  

    $scope.getResult = function() {

        GetResultFile.getResult(id, rid)
        .then(function(data) {
            console.log("Download erfolgreich");
            console.log("data.status");
        });
    }, function(data) {
        console.log("Download ERROR!");
        console.log(data.status);
    };                  

編輯

我用這種方法嘗試:

$scope.getResult = function(rid) {

        GetResultFile.getResult($scope.id, rid)
        .then(function(data) {
            console.log("Download erfolgreich");
            console.log("data.status");
        });
    }, function(data) {
        console.log("Download ERROR!");
        console.log(data.status);
    };  

這是我認為從$ scope.upload進行的調用:

<span ng-show="dataTable">
    <table>
        <tr ng-repeat="r in results">
            <td><a ng-click="getResult(r.id)" download>{{r.results.name}}</a></td>
        </tr>
    </table>
</span>

這是從$ scope.getResult:

<span ng-show="dataTable">
    <table>
        <tr ng-repeat="r in results">
            <td><a ng-click="getResult(r.id)" download>{{r.results.name}}</a></td>
        </tr>
    </table>
</span>

是否可以將$ scope變量傳遞給控制器​​內部的另一個函數?

$scope作為參數注入到控制器函數中,因此控制器內定義的所有函數都可以通過閉包訪問$scope

.controller('mycontroller', function($scope) {

    function makesPostRequest() {
       console.log($scope);
    }

    $scope.getResult = function(params) {
       console.log($scope);
    }

});

因此,以您的示例為例,應該這樣做:

$scope.getResult = function (rid) {
    GetResultFile.getResult($scope.id, rid)
                            ^^^^^^^^^
        .then(function (data) {
            console.log("Download erfolgreich");
            console.log("data.status");
        }, function (data) {
            console.log("Download ERROR!");
            console.log(data.status);
        })
};

暫無
暫無

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

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