如何将一个对象从一个函数发送到angularjs中的另一个函数?

时间:2022-05-21 07:19:34

I am new to AngularJS and Javascript. I tried to send an object from one function to another function

我对AngularJS和Javascript不熟悉。我试图将一个对象从一个函数发送到另一个函数

HTML Code:

HTML代码:

<div ng-click="getValueFromHtml(userObj)">send Object </div>

Controller Code:

控制器代码:

$scope.getValueFromHtml=function(userObj){
console.log(JSON.stringify(userObj)); //here userObj coming from html
}

$scope.getUserObj=function(userObj){
console.log(userObj) //here i want to use userObj
}

1 个解决方案

#1


2  

You can stored the userObj in local to the controller.

您可以将userObj存储在控制器的本地。

.controller('testCtrl',function(){

    var objUser = null;
    scope.getValueFromHtml=function(userObj){
        objUser = userObj;
    }

    $scope.getUserObj=function(userObj){
        console.log(objUser) //here i want to use userObj
    }
})

In the future,if you want to share this one across app,

未来,如果你想在应用中分享这个,

you can stored it using services/factory.

您可以使用服务/工厂存储它。

#1


2  

You can stored the userObj in local to the controller.

您可以将userObj存储在控制器的本地。

.controller('testCtrl',function(){

    var objUser = null;
    scope.getValueFromHtml=function(userObj){
        objUser = userObj;
    }

    $scope.getUserObj=function(userObj){
        console.log(objUser) //here i want to use userObj
    }
})

In the future,if you want to share this one across app,

未来,如果你想在应用中分享这个,

you can stored it using services/factory.

您可以使用服务/工厂存储它。