简体   繁体   English

我可以将$ scope变量从一个函数传递到控制器内的另一个函数吗? AngularJS

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

is it possible to pass a $scope variable to another function inside the controller? 是否可以将$ scope变量传递给控制器​​内部的另一个函数?

the first function is a POST request which included the id. 第一个功能是包含ID的POST请求。

the second function is a ng-click function which needs the $scope.id variable. 第二个函数是ng-click函数,需要$ scope.id变量。

is there a way to pass them ? 有办法通过它们吗? maybe as a global variable or can i write the $scope.id in the parameter list from the second function. 也许作为全局变量,或者我可以在第二个函数的参数列表中写入$ scope.id。

the second approach is that I pass the parameter in my view with ng-click = "getResult({{id}}, r.id)" 第二种方法是使用ng-click =“ getResult({{id}},r.id)”在视图中传递参数

this is my controller: 这是我的控制器:

    .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);
    };                  

EDIT 编辑

i try it with this approach: 我用这种方法尝试:

$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);
    };  

this is the call in my view from $scope.upload: 这是我认为从$ 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>

this is from the $scope.getResult: 这是从$ 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>

is it possible to pass a $scope variable to another function inside the controller ? 是否可以将$ scope变量传递给控制器​​内部的另一个函数?

$scope is injected as a parameter to the controller function, so all functions defined inside the controller have access to the $scope through closure $scope作为参数注入到控制器函数中,因此控制器内定义的所有函数都可以通过闭包访问$scope

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

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

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

});

So, given your example, this should do it: 因此,以您的示例为例,应该这样做:

$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.

相关问题 如何将变量从控制器内部的函数传递到控制器作用域? - How to pass variable from function inside controller to controller scope? 我可以将变量从一个 function 传递到另一个 javascript 吗? - Can I pass variable from one function to another in javascript? 如何将变量从一个函数传递给另一个函数? - How can I pass a variable from one function to another? 将范围变量从指令传递给控制器​​功能 - pass scope variable from directive to a controller function 从运行块我可以在另一个控制器而不是$ rootscope内调用角度范围函数 - From run block can i able to call angular scope function inside another controller instead of $rootscope 从angularjs中的另一个控制器调用函数 - Call function from one controller in another in angularjs 如何将数组从服务功能传递到控制器(AngularJs)中的作用域? - How to pass array from service function to scope in controller (AngularJs)? 如何将函数处理程序从控制器传递到AngularJs中的指令隔离范围? - How to pass a function handler from controller into directive isolated scope in AngularJs? 将变量从一个函数传递到另一个函数 - Pass variable from one function to another function Angularjs我可以从另一个控制器更改控制器的$ scope值吗? - Angularjs Can I change $scope value of a controller from another controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM