DOM准备好后如何执行angularJs函数?

时间:2014-10-02 04:18:00

标签: javascript jquery angularjs

我正在使用谷歌地图& angularJS,我想要在JSON文件中超出当前位置和另一个位置之间的距离。但是距离只显示我在视图模板中有一个按钮时,通过ng-click调用角度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>

如何在不点击任何内容的情况下显示距离?

2 个答案:

答案 0 :(得分:0)

只需在与您的视图相关联的控制器中调用此块

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;
}]);

答案 1 :(得分:0)

您可以使用控制器功能执行代码。

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;
        });
    }