AngularJS: How can I pass variables between controllers?

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

I have two Angular controllers:

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

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


        
16条回答
  •  梦毁少年i
    2020-11-22 01:26

    The sample above worked like a charm. I just did a modification just in case I need to manage multiple values. I hope this helps!

    app.service('sharedProperties', function () {
    
        var hashtable = {};
    
        return {
            setValue: function (key, value) {
                hashtable[key] = value;
            },
            getValue: function (key) {
                return hashtable[key];
            }
        }
    });
    

提交回复
热议问题