AngularJS $scope not populating view

I have a view like so:

<div ng-controller= "test" ng-init = "doit()">
    <span>{{testval}}</span>
</div>

The view's controller looks like this:

function test($scope){
    var counter = 0;
    $scope.doit = function(){
       console.log('switched to view');
       $scope.testval = counter++; 
    };
};

The first time I switch to the view controlled by 'test', it shows '0'. After that, it shows nothing, but the console.log continues firing. How can I set this up to execute 'doit' each time I switch to the view and successfully update the view by modifying $scope?

Thanks

Remove the ng-init() and put the call to doit() into your controller:

function test($scope){
    var counter = 0;
    $scope.doit = function(){
       console.log('switched to view');
       $scope.testval = counter++; 
    };
    $scope.doit();
};

Each time you switch to this view, the controller code will execute. If you want the counter to persist between view changes, you'll have to put it into a service or $rootScope (because the controller, well its $scope, is destroyed when you move to another view).

Had to add $scope.digest() to the end of doit. The example listed is a boiled down example of the logic I was encountering on a much smaller scale. In reality, it was a function called getPhysOrd that executed an asynchronous templated websocket bridge to query data off the server. It had to be able to do this on-the-fly. Calling digest forced the watchers to update the view according to the model changes. Thanks