简体   繁体   English

angularjs调用另一个控制器的功能

[英]angularjs call another controller's function

I have a ons-list with the following ons-items : 我有一个包含以下ons-items的ons-list:

<ons-list-item style="font-family:roboto;font-size:17px" ng-class="{'selected-menu':selectedRow==0,'no-selected':selectedRow!=0}"
        modifier="tappable" class="list__item__line-height"
        onclick="app.slidingMenu.setMainPage('principal.html', {closeMenu: true})"
        ng-click="principal(0)">
        <i class="fa fa-home fa-lg"></i>
        &nbsp;Principal
</ons-list-item>

<ons-list-item style="font-family:roboto;font-size:17px" ng-class="{'selected-menu':selectedRow==1,'no-selected':selectedRow!=1}"
        modifier="tappable" class="list__item__line-height"
        onclick="app.navi.pushPage('ingreso.html', { animation : 'slide' } );app.slidingMenu.closeMenu()"
        ng-click="principal(1)">
        <i class="fa fa-car fa-md"></i>
        &nbsp;Nuevo Registro
</ons-list-item>

etc... 等等...

and I have a function called principal() that what it does is change the "$scope.selectedRow" so the item changes classes. 并且我有一个称为principal()的函数,它的作用是更改“ $ scope.selectedRow”,以便该项目更改类。 I need to access that function so i can change the class from another controller, i've check some answers from other questions but I haven't been able to do it right, is there a way to acomplish this? 我需要访问该函数,以便可以从另一个控制器更改类,已经检查了其他问题的一些答案,但是我无法正确完成操作,有没有办法做到这一点?

this is my principal() function: 这是我的Principal()函数:

$scope.principal = function(item){
    $scope.selectedRow = item;
}

add the function in your controller that you are using. 在正在使用的控制器中添加功能。 And than give a button a ng-click like this 而不是像这样给按钮一个ng-click

ng-click="principal(item)"

As suggested by SSH i ended up using broadcast and on so the credit goes to him, what I did was: 正如SSH所建议的那样,我最终使用广播,因此功劳归功于他,我所做的是:

declare my controller like: 声明我的控制器,例如:

module.controller('Ingreso', ['$scope','$rootScope',function($scope,$rootScope){

inside of it a broadcast the event: 在其中广播事件:

$rootScope.$broadcast("ingresing");

and from the menu controller(same declaration): 并从菜单控制器(相同的声明):

module.controller('Principal', ['$scope','$rootScope', function($scope,$rootScope) {

and listen to the briadcasted event: 并收听广播节目:

$rootScope.$on("ingresing", function(){
    $scope.selectedRow = 1;
}); 

Thank you very much for the help! 非常感谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM