仍未通过ng-click

时间:2017-02-10 09:20:26

标签: angularjs accordion angularjs-ng-click

以下是我使用的HTML

<div ng-controller="profileEventsCtrl" >
<div class="row mainNavbar" data-ng-show="hideVanityPlate">
<div id="navigatornavbarview" data-ui-view="navigatornavbarview"></div>
</div>
<div class="Profileindex" ng-init="init()">
<div id="profile" style="padding-bottom:20px">
    <div class="col-md-12"> <h1>My Events</h1></div>
    <div class="row">
        <div class="col-md-2">
            <div profile-navigation=""></div>
        </div><!--col-md-2-->
        <div class="col-md-10">
            <accordion close-others="true">
                <accordion-group is-open="status.open" ng-repeat="userEvent in userEvents">
                    <accordion-heading>
                        {{userEvent.Event.Title}}
                        <div class="condensed medium pull-right date">{{userEvent.Event.FormatedEventDateStartToEnd}} &nbsp</div>
                        <div class="condensed medium lineblock">
                            <span class="place ">{{userEvent.Event.Location}}</span> |  <span class="place" ng-click="switchEvent(userEvent.Event.Code)" ><span>{{SwitchEventText}}</span></span>
                        </div>
                    </accordion-heading>
                </accordion-group>
            </accordion>
        </div>
    </div>
</div>

这是我使用的控制器

(function (ng, app) {
'use strict';

app.controller('profileEventsCtrl', ['$scope', '$location', '$state', '$rootScope', '$q', '$timeout', 'localization','profileSvcs', profileEventsCtrl]);
function profileEventsCtrl($scope, $location, $state, $rootScope, $q, $timeout, localization, profileSvcs) {
    $scope.userEvents = {};
    $scope.SwitchEventText = "Switch Events"

    $scope.init = function () {
        $rootScope.currentScope = $scope;
        $scope.hideVanityPlate = true;
        $scope.getMyEvents();

    };


    $scope.getMyEvents = function () {
        profileSvcs.getMyEvents().then(function (response) {
            $scope.userEvents = response.data;
        });
    }

    $scope.switchEvent = function (eventCode) {
        var newLocationHash = "/" + $rootScope.cultureCode + "/navigator/" + eventCode + "/profile";
        $location.path(newLocationHash);
    }



}    
})(angular, app);

所有其他组件都运行良好。数据从API加载并填充。但是ng-click不起作用。我没有事件得到任何控制台错误,因此难以调试。

还要测试函数switchEvent是否在范围内。我尝试ng-init而不是ng-click,并为ng-repeat中的每一行调用函数,因此我假设switchEvent在范围内。

3 个答案:

答案 0 :(得分:0)

尝试使用ng-click传递对象并访问click函数

中的属性
ng-click="switchEvent(userEvent)" 

<强>控制器:

  $scope.switchEvent = function (eventCode) {
        var newLocationHash = "/" + $rootScope.cultureCode + "/navigator/" + eventCode.Event.Code + "/profile";
        $location.path(newLocationHash);
    }

答案 1 :(得分:0)

见这一行: -

<span class="place ">
    {{userEvent.Event.Location}}
</span> |  
<span class="place" ng-click="switchEvent(userEvent.Event.Code)">
   <span>
      {{SwitchEventText}}
   </span>
</span>

根据你在哪里点击调用此方法

它可能没有被调用,因为你没有点击确切的跨度。可能是......

尝试将其更改为

<span class="place ">
    {{userEvent.Event.Location}}
</span> |  
<span class="place" ng-click="switchEvent(userEvent.Event.Code)">
   {{SwitchEventText}}
</span>

删除内部范围

答案 2 :(得分:0)

将accordion.js替换为http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.2.0.js,它对我有用。