javascriptangularjsangularjs-scopeangularjs-ng-touch

angularjs, Scope not updating across functions - with ngTouch


I have a controller with such:

$scope.myVar = 0;

  $scope.back = function () {
    $scope.myVar--;
  };

  $scope.next = function () {
    $scope.myVar++;
  };

If next() (with ngClick) is called 3 times, we get:

//1

//2

//3

but if back() (with ngSwipeLeft) is called it returns

//-1

when I'm obviously expecting

//2

What am I missing here?

update: including ngTouch details - this seems to be the problem.. ngTouch is included.

When I watch the myVar value - its like it exists twice - one with the ngSwipeLeft call, and one with the ngClick call


Solution

  • Ok, so I've figured out my problem - I wasn't providing enough detail in the question - but if someone runs into something similar in the future, heres what was going on:

    I was declaring my controller with ng-controller="myCtrl" in the templates, but also using routing, where I declared my controller like:

    $routeProvider.when('/', {
    templateUrl: 'myUrl.html',
    controller: 'myCtrl'
    });
    

    This was instantiating the controller twice, and obviously causing problems (although that seemed to the only one to surface for now).

    Removing the controller definition from the routing or the view did the trick.