-1

Angular doesn't recognize that when i put together model (string) and $scope to form $scope.model that it is indeed a defined scope variable. Is there anyway to not make model a string so $scope.model will actually refer to what I need it to refer to?

$scope.games = [];
    $scope.maxplayers = 0;
    $scope.minplayers = 0;

........

function setupWatcher(model){

  $scope.$watch(model,function(){

    console.log($scope.model);  // prints out undefined
    if(!$scope.model) return;




    $http.get('/api/games?'+ model + '=' + $scope.model)
      .success(function(data){
        $scope.games = data;

      })
      .error(function(data){
        console.log('Error' + data);
      });

  });
}

angular.forEach(['maxplayers',' minplayers'],setupWatcher);
user2510809
  • 235
  • 3
  • 14

2 Answers2

0

Yes, you should do it like this

$scope[model]

You can access object properties either way

var propName = 'prop3',
  myObject = {
    prop1: "123",
    prop2: "456",
    prop3: "789"
};

myObject.prop1 // 123
myObject['prop2'] // 456
myObject[propName] // 789
matewka
  • 9,912
  • 2
  • 32
  • 43
0

Use bracket notation:

$scope[model]
tymeJV
  • 103,943
  • 14
  • 161
  • 157