2017-04-17 85 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,因此无法访问。