簡體   English   中英

使用angularjs將數據傳遞到同一控制器中的一個函數中的另一個函數

[英]Passing data into one function to another function in same controller using angularjs

我正在使用兩個功能來存儲開始停止時間,使用此代碼,

$scope.start = function(data) {

    $scope.time = {
        id: '',
        mainId: data.id,
        start: ''
    }
    $scope.Mydata = data;

    function startTime() {
        $http.post('/api/timestart', $scope.time).then(function(response) {

        });
    }
    startTime();

}



$scope.stop = function(data) {
    $scope.time = {
        id: '',
        mainId: data.id,
        start: '',
        stop: moment(new Date())
    }


    $scope.Mydata = data;

    function stopTime() {
        $http.post('/api/timestop', $scope.time).then(function(response) {

        });
    }

    stopTime();
}

我想返回並將start()函數數據傳遞給stop()函數。

使用angularjs將數據傳遞到同一個控制器中的另一個函數中。

只需調用$scope.stop()

function startTime() {
        $http.post('/api/timestart', $scope.time).then(function(response) {
            $scope.stop(response);
        });
}

您已經在函數內部創建了函數。 請在$scope.start()$scope.stop()函數中刪除函數startTime(){}function stopTime() {}

您的代碼應該像

$scope.start = function(data) {
$scope.time = {
        id: '',
        mainId: data.id,
        start: ''
    }
    $scope.Mydata = data;

    $http.post('/api/timestart', $scope.time).then(function(response) {
    $scope.stop(response);
     });
}
$scope.stop = function(data) {
    $scope.time = {
        id: '',
        mainId: data.id,
        start: '',
        stop: moment(new Date())
    }
    $scope.Mydata = data;

    $http.post('/api/timestop', $scope.time).then(function(response) {

    });
   }

暫無
暫無

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

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