[英]How to execute an angularJs function after DOM is ready?
我正在使用Google Map&angularJS,我想执行当前位置和JSON文件中另一个位置之间的距离。 但是距离仅在视图模板中有一个按钮时显示,该按钮通过ng-click在angular JS中调用一个函数:
<button id="show-distance" ng-click="showDistance()">Show Distance</button>
在AngularJS文件中:
$scope.showDistance = function(){
jQuery.each($scope.items, function (index, value) {
$scope.distance = $scope.getDistance(jQuery.cookie('currentLat'), jQuery.cookie('currentLng'), value['latitude'], value['longitude']);
value['location'] = $scope.distance;
});
}
我以这种方式显示距离:
<table style="border: 1px solid" class="data-table">
<tr>
<th><?php echo $this->__('Store name')?></th>
<th><?php echo $this->__('Address')?></th>
<th><?php echo $this->__('Distance')?></th>
</tr>
<tr ng-repeat="item in items | filter:query | orderBy:orderProp | limitTo:quantity " ng-controller="ItemController" class="list-locator">
<td ng-click="open(item)">{{item.title}}</span></td>
<td ng-click="open(item)">{{item.address}}</td>
<td ng-click="open(item)">{{item.location}} miles</td>
</tr>
</table>
我可以做些什么来显示距离而无需单击任何东西?
只需在与您的视图绑定的控制器中调用此块
var myApp = angular.module('myApp',[]);
myApp.controller('SomeController', ['$scope', function($scope) {
jQuery.each($scope.items, function (index, value) {
$scope.distance = $scope.getDistance(jQuery.cookie('currentLat'), jQuery.cookie('currentLng'), value['latitude'], value['longitude']);
value['location'] = $scope.distance;
}]);
您可以使用控制器功能来执行代码。
function MainControl($scope) {
angular.element(document).ready(function () {
$scope.distance = $scope.getDistance(jQuery.cookie('currentLat'), jQuery.cookie('currentLng'), value['latitude'], value['longitude']);
value['location'] = $scope.distance;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.