2015-12-15 31 views
0

我的console.log語句顯示$ scope.selectedUser在條件期間有一個值,在我的函數$ scope.showUserPets中沒有定義。這怎麼可能,我該如何解決?

var myApp = angular.module("Pets"); 
myApp.controller("MainCtrl", function($scope, $routeParams, $http) { 
    $scope.selectedUser = $routeParams.user; 
    if ($scope.selectedUser && $scope.info) { 
     console.log("Has user and doing showUserPets: " + $scope.selectedUser); // user = user1 
     $scope.showUserPets(); 
    } 

    $scope.init = function() { 
     // Sets $scope.info via $http.get 
    } 

    $scope.showUserPets() = function() { 
     for (var i = 0; i < ($scope.info).length; i++) { 
      console.log($scope.selectedUser + " " + $scope.info[i].user); // $scope.selectedUser is undefined. 
     } 
+0

在$ scope.showUserPets()=函數(){...刪除()在$ scope.showUserPets()...使用$範圍。 showUserPets = function(){... –

回答

0

試試這個:

$scope.showUserPets($scope.selectedUser); 



    /* notice that in you code $scope.showUserPets() ... don't use() in the 
    $scope variable */ 
    $scope.showUserPets = function(selected) { 
     for (var i = 0; i < ($scope.info).length; i++) { 
      console.log(selected + " " + $scope.info[i].user); // $scope.selectedUser is undefined. 
     }