0

我正在使用 angularJS1.2 路由功能来部分加载一些视图。

var app = angular.module('app', ['ngAnimate', 'ngRoute']).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.when('/profile/personalInfo', {templateUrl: '<?php echo base_url().'profile/personalInfo' ?>', controller: personalInfo});
        $routeProvider.when('/profile/myPlaces', {templateUrl: '<?php echo base_url().'profile/myPlaces' ?>', controller: myPlaces});
        $routeProvider.otherwise({redirectTo: '/profile/personalInfo'});
        $locationProvider.html5Mode(true);
    }]);

 function personalInfo($scope, $http){
      console.log('personal info');
  }

  function myPlaces($scope, $http){
      console.log('my places');
      $scope.places = [];
      //then fill places via $http.post request
      $scope.loadMore = function(){
         console.log('clicked');
      }
  }

这是我的平页:

<div>
    <h1>My Places Page</h1>
    <div ng-repeat="place in places">
        <a href="" ng-click="loadMore()">load more</a>
    </div>
</div>

路由已成功完成,但是当我单击链接调用loadMore时,控制台中出现此错误undefined is not a function。有什么问题,我该如何解决?

4

2 回答 2

1

我找到了一个解决方案,但我不确定这个解决方案是否是最佳的,但至少它解决了问题。我创建了一个自定义指令,如下所示

app.directive('whenClicked', function() {
        return function(scope, elm, attr) {
            var raw = elm[0];
            angular.element(raw).bind('click', function() {
                console.log('clicked');
            });
        };
    });

然后从部分页面你可以调用指令

<div>
    <h1>My Places Page</h1>
    <div ng-repeat="place in places">
        <a href="" when-clicked>load more</a>
    </div>
</div>
于 2013-10-28T12:57:06.080 回答
0

使其成为控制器:

app.controller('MyCtrl',function myPlaces($scope, $http){
  console.log('my places');
  $scope.places = [];
  //then fill places via $http.post request
  $scope.loadMore = function(){
     console.log('clicked');
  }
 });
于 2013-10-28T09:45:16.803 回答