0

I've got a employeeController. That employeeController has several functions.

For example:

login 
getEmployee

This is (for example)my code:

   function employeeController()
    {
        var vm = this;
            vm.login = function() {
                //login logic
            }
            vm.getUser = function(id) {
                //receive one user
            }
        vm.getUser();
    }

When a employee visits his/her page the employee data must load immediately. But when I login I don't want the vm.getUser() method be called. How could I get this right!?

Jamie
  • 10,302
  • 32
  • 103
  • 186
  • You are following older way and referring older angular version may be.. refer [this answer](http://stackoverflow.com/a/28728380/2435473) would help you. – Pankaj Parkar Mar 23 '16 at 13:05
  • check for the route. If route after login is home, apply this condition when you call – Ved Mar 23 '16 at 13:06
  • You are invoking the function `getUser` when controller is called, You can't stop that unless you specify something else – Satpal Mar 23 '16 at 13:07
  • 1
    @PankajParkar, I don't think the question is duplicate __I don't want the vm.getUser() to be called__ – Satpal Mar 23 '16 at 13:10
  • @Satpal OP is not responding to our comments, I'm still feeling that he is following old pattern of registering controller globally.. thanks for heads up though.. – Pankaj Parkar Mar 23 '16 at 13:12
  • @PankajParker I'm not using the old way. – Jamie Mar 23 '16 at 13:14

2 Answers2

1

you forgot put $scope in controller.

function employeeController($scope)
    {
           var vm = this;
            vm.login = function() {
                alert("Test");
            }
            vm.getUser = function(id) {
                //receive one user
            }
        vm.getUser();
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-controller="employeeController as ctrl" >
  <input type="button" value="Test" ng-click = "ctrl.login()">
</div>
Hadi J
  • 16,989
  • 4
  • 36
  • 62
-1

Better Use call back function to achieve. Example

function Ctrl($scope, $http, $q) {

  $http({
    method: 'JSONP',
    url: url
  }).success(function(data) {

//    do  get user function
      }

    });


}
SASM
  • 1,292
  • 1
  • 22
  • 44