web - Angularjs how to call function from another controller? -
i have checked of topics matter , got understanding of controllers there initiate scope , need utilize services matter dont know how.
so here problem. have index page body has 1 div , within div have ng-include listening function called viewfile() described on controllera. on first initial effort load view called login.html , display it. when users logs in , successful, handled in controllerb, homecoming token , want load main.html page using viewfile() in controllera. there phone call function or notify controller or this? or can write service takes care of me?
i'm not using ngroute because dont want url alter mysite.com/#/login.html , mysite.com/#/main.html
.controlle("a", function ($scope, sharedvariable){ $scope.token = sharedvariable.gettoken(); $scope.viewfile = function(){ if($scope.token == "") homecoming "view/login.html"; else homecoming "view/main.html"; } } .controller("b", function ($scope, $http, sharedvariable)){ http({ ... .success: function(data){ $scope.token = sharedvariable.settoken(); // invoke viewfile above controller } }) }
and here index.html body part
<body> <div ng-controller="a"><ng-include src="viewfile()"></ng-include></div> </body>
look @ simple illustration http://jsfiddle.net/derkoe/t85rg/presentation/ here personservice.person shared between 2 controllers can write viewfile function in 1 service personservice. phone call personservice.viewfile controller. can pass $scope argumen. below
var mymodule = angular.module('mymodule', []); mymodule.factory('myservice', function($rootscope) { var sharedservice = {}; sharedservice.viewfile = function($scope) { if($scope.token == "") homecoming "view/login.html"; else homecoming "view/main.html"; }; homecoming sharedservice; });
angularjs web angularjs-scope
No comments:
Post a Comment