AngularJS: How can I pass variables between controllers?

后端 未结 16 2585
误落风尘
误落风尘 2020-11-22 00:31

I have two Angular controllers:

function Ctrl1($scope) {
    $scope.prop1 = \"First\";
}

function Ctrl2($scope) {
    $scope.prop2 = \"Second\";
    $scope.         


        
16条回答
  •  名媛妹妹
    2020-11-22 01:08

    Ah, have a bit of this new stuff as another alternative. It's localstorage, and works where angular works. You're welcome. (But really, thank the guy)

    https://github.com/gsklee/ngStorage

    Define your defaults:

    $scope.$storage = $localStorage.$default({
        prop1: 'First',
        prop2: 'Second'
    });
    

    Access the values:

    $scope.prop1 = $localStorage.prop1;
    $scope.prop2 = $localStorage.prop2;
    

    Store the values

    $localStorage.prop1 = $scope.prop1;
    $localStorage.prop2 = $scope.prop2;
    

    Remember to inject ngStorage in your app and $localStorage in your controller.

提交回复
热议问题