在Angular中,为什么ng-click按钮会多次触发?

时间:2015-06-08 00:27:39

标签: javascript angularjs angularjs-ng-click

我的应用中有一些按钮,如下所示:

<ul class="gallery-buttons">
    <li><button class="video-gallery" ng-click="getGallery('video',{{install.id}})"></button></li>
    <li><button class="image-gallery" ng-click="getGallery('image',{{install.id}})"></button></li>
</ul>

我的控制员:

var nanoApp = angular.module('nanoApp', ['ngRoute', 'ngCordova']);
nanoApp
.config(function ($routeProvider, $compileProvider) {
    ...code to setup all my routes using the $routeProvider...
})
.controller('InstallationListController', ["$scope", "$cordovaSQLite", "$rootScope", function ($scope, $cordovaSQLite, $rootScope) {
    document.addEventListener("deviceready", function () {
        ...do some database calls and setup initial $scope...
        $scope.getGallery = function (galleryType, installId) {
            console.log('made it');
        }
    });

});

我的按钮属于controller$scope,其中包含ng-repeat个41个元素。当我选择其中一个时,我得到console.log 41次。即使我将按钮移到ng-repeat之外,所以只有一组,它仍会发射41次。

--- 编辑 ---

关于我的ng-repeat的信息,请注意这是我的$routeProvier.when中提取的模板,控制器是在那里声明的,而不是在容器上:

<div id="installationlist" class="container">

    <div class="install" section-width directive-scroll-to="{{moveframe}}" get-products install-index="{{$index}}" ng-repeat="install in installations track by $index">
        <div class="details">
            <div class="text">
                <div class="text-container">

                    <p class="industry">Industry: {{install.industry}}</p>
                    <p class="title">{{install.name}}</p>

                    <p class="location">{{install.location}}</p>

                    <div class="product-block">
                        <p class="product" ng-repeat="product in install.products track by $index">{{product.name}}</p>
                        <p class="pixelPitch" ng-if="install.pixelPitch != null">{{install.pixelPitch}}</p>
                    </div>

                    <p class="desc">{{install.instText}}</p>

                </div>
            </div>

            <ul class="gallery-buttons">
                <li><button class="video-gallery" ng-click="getGallery('video',install.id)"></button></li>
                <li><button class="image-gallery" ng-click="getGallery('image',install.id)"></button></li>
            </ul>
        </div>

        <div class="image" style="background-image:url('img/products/nanoslim-bkg.jpg')"></div>
    </div>

</div>

0 个答案:

没有答案