2017-04-17 87 views
-1

定義的錯誤在我HomeCtrl.js我從另一個功能initialize()調用函數currentLocation(),我定義這個機能的研究,但它給我的功能錯誤沒有被定義的:\ 代碼更新但仍同樣的錯誤發生 有人可以告訴我什麼是我的代碼中的問題?角JS:功能未在控制器

HomeCtrl.js

'use strict'; 
angular.module('Home').controller('HomeCtrl',['$scope','$state','MessageService', function($scope, $state, $ionicModal, MessageService) { 

    (function initialize(){ 
     var location = $scope.currentLocation(); 
     $scope.mapOptions = { 
     mapTypeControl: true, 
     zoom: 15, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: location 
     }; 
    })(); 
$scope.currentLocation = function(){ 

navigator.geolocation.getCurrentPosition(function(pos) { 
       $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
       return $scope.position;    
      }); 
}; 

}]); 

回答

1

把它放在自調用函數

'use strict'; 
angular.module('Home', []).controller('HomeCtrl', function($scope) { 
    $scope.currentLocation = function() { 

     navigator.geolocation.getCurrentPosition(function(pos) { 
      $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
      return $scope.position; 
     }); 
    }; 

    (function initialize() { 
     var location = $scope.currentLocation(); 
     $scope.mapOptions = { 
      mapTypeControl: true, 
      zoom: 15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      center: location 
     }; 
    })(); 

}); 

'use strict'; 
 
angular.module('Home', []).controller('HomeCtrl', function($scope) { 
 
    $scope.currentLocation = function() { 
 
    console.log("working") 
 

 
     navigator.geolocation.getCurrentPosition(function(pos) { 
 
      $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
 
      return $scope.position; 
 
     }); 
 
    }; 
 

 
    (function initialize() { 
 
     var location = $scope.currentLocation(); 
 
     $scope.mapOptions = { 
 
      mapTypeControl: true, 
 
      zoom: 15, 
 
      center: location 
 
     }; 
 
    })(); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="Home" ng-controller="HomeCtrl"> 
 
    
 
</div>

1

您應該已經向空的依賴到你的模塊

變化

angular.module('Home').controller('HomeCtrl', 

angular.module('Home',[]).controller('HomeCtrl', 

另外的參數的順序是錯誤的,它改變爲

'use strict'; 
angular.module('Home').controller('HomeCtrl', ['$scope', '$state','$ionicModal', 'MessageService', function ($scope, $state, $ionicModal, MessageService) { 
    $scope.currentLocation = function() { 
console.log("working") 

    navigator.geolocation.getCurrentPosition(function(pos) { 
     $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
     return $scope.position; 
    }); 
}; 

(function initialize() { 
    var location = $scope.currentLocation(); 
    $scope.mapOptions = { 
     mapTypeControl: true, 
     zoom: 15, 
     center: location 
    }; 
})(); 
}]); 
+0

我有更新我的代碼,請檢查頂部...還是同樣的錯誤內容時發生 –

+0

@mariasalahuddin與更新檢查控制器在答案 – Sajeetharan

1

$scope.currentLocation定義應該是內部模塊。

當前您正在模塊外編寫currentLocation,因此無法訪問。