简体   繁体   English

单击链接时未调用AngularJs控制器

[英]AngularJs controller not getting called when a link is clicked

Currently, I'm working on an initial AngularJs app for an Admin Console for a database. 目前,我正在为数据库的管理控制台开发最初的AngularJs应用程序。 I'm pretty new at Angular, so I've started off by modifying the phonecat tutorial. 我在Angular还很新,所以我从修改phonecat教程开始。 Basically, I've got a Jersey REST API that I need to make calls against in Angular. 基本上,我有一个Jersey REST API,需要在Angular中进行调用。 CORS is not the problem, they're both running on the same Tomcat. CORS并不是问题,它们都在同一个Tomcat上运行。

The data structure is Projects have Groups within them. 数据结构是其中包含组的项目。

I can redirect from Login to the Projects list page, the controller for getting the list of projects is called, and I can click a project and get the details page and its controller is called. 我可以从“登录”重定向到“项目”列表页面,调用用于获取项目列表的控制器,然后单击一个项目并获取详细信息页面,然后调用其控制器。 But when I click a link from the project-details page and have it redirect to the groups-list page, the REST api call is never made (according to Chrome's network monitor.) 但是,当我单击“项目详细信息”页面上的链接并将其重定向到“组列表”页面时,就永远不会进行REST api调用(根据Chrome的网络监视器)。

I'm rather confused because hypothetically it should be the exact same as the projects list. 我很困惑,因为假设它应该与项目列表完全相同。

Controllers: 控制器:

phonecatControllers.controller('ProjectListCtrl', ['$scope', '$http',function($scope, $http){

$http.get('http://localhost:8080/WrappingServer/rest/api/project?show_hidden=true').success(function(data){
    $scope.projects = data.hits.hits

});
$scope.orderprop='timeModified';
}]);

phonecatControllers.controller('GroupListCtrl', ['$scope', '$http',function($scope, $http){

$http.get('http://localhost:8080/WrappingServer/rest/api/group?projectID='+$scope.project._id+'&show_hidden=true').success(function(data){
    $scope.groups = data.hits.hits //Yes this is correct. It's an elasticsearch cluster.

});
$scope.orderprop='timeModified';
}]);

phonecatControllers.controller('ProjectDetailCtrl', ['$scope', '$routeParams' ,'$http', function($scope, $routeParams, $http){
$http.get('http://localhost:8080/WrappingServer/rest/api/project/'+$routeParams.projectId).success(function(data){
    $scope.project=data;

});

}]);

project-details.html: project-details.html:

<img ng-src="{{project._source.imageUrl}}" class="project">
<h1>{{project._source.name}}</h1>
<p>{{project._id}}</p>
<ul class="specs">
    <a ng-href="#/groups" ng-click=>Group IDs</a>
     <dd ng-repeat="group in project._source.groupIDs"><a href="#/projects/{{proj._id}}">{{proj._source.name}}</a>
    {{group.groupID}}
    </dd>
  </ul>

group-list.html group-list.html

  <div class="container-fluid">
          <div class="row">
            <div class="col-md-2">
      <!--Sidebar content-->

      Search: <input ng-model="query">
      Sort by:
<select ng-model="orderprop">
  <option value="name">Alphabetical</option>
  <option value="dateModified">Newest</option>
</select>

    </div>
    <div class="col-md-10">

      <!--Body content-->

      <ul class='groups'>
<li ng-repeat="group in groups | filter:query | orderBy:orderprop" class="thumbnail">
<a href="#/groups/{{group._id}}" class="thumb"><img ng-src="    {{group._source.imageUrl}}" alt="{{group._source.name}}"></a>
            <a href="#/groups/{{group._id}}">{{group._source.name}}</a>
            <p>{{group._source.timeModified}}</p>
</li>
</ul>


    </div>
  </div>
</div>

app.js app.js

'use strict';

/* App Module */

var phonecatApp= angular.module('phonecatApp', [
    'ngRoute', 'phonecatControllers', 'phonecatFilters']



);

phonecatApp.config(['$routeProvider',
    function($routeProvider){
        $routeProvider.when('/projects', 
        {
            templateUrl: 'partials/project-list.html',
            controller: 'ProjectListCtrl'
        }).when('/projects/:projectId', {
            templateUrl: 'partials/project-detail.html',
            controller: 'ProjectDetailCtrl'
        }).when('/home', {
            templateUrl: 'partials/login.html',
            controller: 'LoginCtrl'
        }).when('/groups',
        {
            templateUrl: 'partials/group-list.html',
            controller: 'GroupListCtrl'
        })




        .otherwise({
            redirectTo: '/home'
        });
        }]);

I've even tried to take an approach where the projectID is a routeParam (kind of like for Project details), but still no luck. 我什至尝试采用一种方法,其中projectID是routeParam(有点像Project的详细信息),但还是没有运气。

Let me know if you anything else to answer my question. 让我知道您是否还有其他答案。 I know it's going to be something so stupidly simple that a handprint will appear on my forehead. 我知道这太简单了,以至手印会出现在我的额头上。 (From what I've seen, possibly a space where it's not supposed to be.) (根据我所看到的,可能是一个不应有的空间。)

Thanks! 谢谢!

<a href="#/groups/{{group._id}}" class="thumb"> should be <a ng-href="/groups/{{group._id}}" class="thumb"> <a href="#/groups/{{group._id}}" class="thumb">应为<a ng-href="/groups/{{group._id}}" class="thumb">

Also <a ng-href="#/groups" ng-click=>Group IDs</a should not have an empty ngClick on it. 另外, <a ng-href="#/groups" ng-click=>Group IDs</a不应有空的ngClick。 It should look like <a ng-href="/groups"> . 它看起来应该像<a ng-href="/groups">

So basically just change the attribute of the link from href to ng-href and then remove the # symbol from the link. 因此,基本上只需将链接的属性从href更改为ng-href,然后从链接中删除#符号。

You have: 你有:

phonecatControllers.controller('GroupListCtrl', ['$scope', '$http',function($scope, $http){
  $http.get('http://localhost:8080/WrappingServer/rest/api/group?projectID='+$scope.project._id+'&show_hidden=true').success(function(data){
    $scope.groups = data.hits.hits //Yes this is correct. It's an elasticsearch cluster.
  });
  $scope.orderprop='timeModified';
}]);

I would instead do this in your controller: 我会在您的控制器中执行此操作:

phonecatControllers.controller('GroupListCtrl', ['$scope', '$http',function($scope, $http) {

  $scope.function_name = function() {
    $http.get('http://localhost:8080/WrappingServer/rest/api/group?projectID='+$scope.project._id+'&show_hidden=true').success(function(data){
      $scope.groups = data.hits.hits //Yes this is correct. It's an elasticsearch cluster.
    });
  }

  $scope.orderprop='timeModified';

}]);

For your link that is within the HTML element possessed by the controller, 对于控制器拥有的HTML元素内的链接,

<a ng-href="/groups" ng-click="function_name()">Group IDs</a>

As it is, your AJAX call is just sitting there. 实际上,您的AJAX呼叫正坐在那里。 You need to place it within a block where code is executed for it to be called, just like in any other case. 就像在任何其他情况下一样,您需要将其放置在执行代码的块中才能对其进行调用。 In this case, you must place it within a function that serves as a callback tied to ng-click. 在这种情况下,必须将其放置在用作与ng-click关联的回调的函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM