2014-10-30 48 views
0

我有兩個控制器,如下圖。我想用第一控制器方法/變量在其他控制器AngularJs我如何使用一個控制器方法,其他控制器中的變量

app.controller("createController", ["$scope", 
function ($scope) 
{ 
    $scope.i = 0; 
    $scope.changeEstimateStatus = function() 
    { 
    console.log('changeEstimateStatus'); 
    }; 
}]); 


app.controller("editController", ["$scope", 
function ($scope) 
{ 
    //here how can I access 'i' variable of createController 
}]); 
+0

控制器如何相關?父母/孩子 - 單獨頁面?提供更多的信息!\ – tymeJV 2014-10-30 13:07:05

+2

似乎是使用具有變量i的服務的地方,然後將其注入到控制器中 – user2717954 2014-10-30 13:09:32

+1

單獨頁面。請不要使用$ rootScope – 2014-10-30 13:09:42

回答

3

使用共享服務:

app.service("mySharedService", [function() { 
    var _x = null; 

    return { 
     setX: function(val) { _x = val }, 
     getX: function() { return _x } 
    } 
}]); 

然後注入到你的控制器:

app.controller("createController", ["$scope","mySharedService", function($scope, mySharedService) { 

    $scope.i = mySharedService.getX(); //get 
    mySharedService.setX(3); //set 
}]); 
+0

Thnx非常適合您的寶貴信息。如果我有更多的方法和變量,請建議任何其他解決方案... – 2014-10-30 13:27:02

0

使用這一個:

1.you可以使用serveice並提出共同作用於服務和訪問,其功能是所有controlller。

app.service('MyService', function() { 
    this.changeEstimateStatus = function() 
     { 
     console.log('changeEstimateStatus'); 
     }; 
}); 

    app.controller("createController", ["$scope",MyService, 
    function ($scope,MyService) 
    { 
     $scope.i = 0; 
    MyService.changeEstimateStatus(); 
    }]); 


    app.controller("editController", ["$scope", app.controller("createController", ["$scope",$rootScope, 
    function ($scope,$rootScope) 
    { 
     $scope.i = 0; 
     MyService.changeEstimateStatus(); 

    }]); 

2.您可以將該函數存儲在$ rootscope object中,然後將該函數訪問到所有控制器。

喜歡:

app.controller("createController", ["$scope",$rootScope, 
function ($scope,$rootScope) 
{ 
    $scope.i = 0; 

}]); 


app.controller("editController", ["$scope",$rootScope, 
function ($scope,$rootScope) 
{ 
    $rootScope.changeEstimateStatus(); 
}]); 

,但第二個選項是不是一個很好的形式給出。

+0

我同意使用服務。這很高興知道爲什麼。在我看來,最好使用一個服務,因爲那樣你就必須在需要它的地方明確地導入這個依賴關係。如果您使用根範圍,則該方法將在任何地方都可用。 – BenCr 2014-10-30 13:16:15

+1

@BenCr服務始終是放置應該在多個控制器之間共享的代碼的正確位置,這正是OP所需的。應儘可能避免$ rootScope,因爲它與應用程序的每一片共享,而服務只能在需要時注入。 – Blazemonger 2014-10-30 13:17:14

+0

這不完全是我說的嗎?我所做的一點是,如果不告訴某人爲什麼不提供高質量的答案,只是說「這不是一個好方法」。 – BenCr 2014-10-30 13:26:42

0

你應該將通用的功能(changeEstimateStatus)到服務。兩個控制器取決於服務,而不是一個控制器。

app.service('estimateService', function() { 

    this.changeEstimateStatus = function() { 
    console.log('changeEstimateStatus'); 
    }; 

}); 
    app.controller("createController", ["$scope","estimateService", 
function ($scope, estimateService) 
{ 
    $scope.i = 0; 
    $scope.changeEstimateStatus = function() 
    { 
    estimateService.changeEstimateStatus(); //delegate to the service 
    }; 
}]); 


app.controller("editController", ["$scope", "estimateService" 
function ($scope, estimateService) 
{ 
    $scope.changeEstimateStatus = function() 
    { 
    estimateService.changeEstimateStatus(); //delegate to the service 
    }; 
}]); 

另一種選擇是使用$ rootScope,但使用服務並沒有變得脆弱。

相關問題