简体   繁体   中英

ng-click inside ng-repeat not working

I am using routeProvider to route to "views/userspace.html" which uses "UserspaceController"

The "views/userspace.html" recognizes this controller and makes request a $http request. This request works file and i can see values in "views/userspace.html" (inside ng-repeat). However, in the same controller i have defined $scope.videoClick function which does not seems to work. Any idea why?

   var application = angular.module("tss.application", 
    [
        "ngRoute",
        "ui.bootstrap"
    ]);

    application.config(function($routeProvider) 
    {
      $routeProvider
          .when("/", {
            templateUrl : "views/main.html",
            controller: "LoginController"
          })
          .when("/files", {
            templateUrl : "views/userspace.html",
            controller: "UserspaceController"
          });
    });

userspace_controller.js

  angular.module('tss.application').controller("UserspaceController",  
    function($scope, $log, $http, $uibModal)
    {       
        $http(
            {
                url     : "/dirlist",
                method  : "GET",
            }).then(function successCallback(response) 
            {
                $scope.lists = response.data;
            }, 
            function errorCallback(response) 
            {
                window.alert("Dir list could not be get");
            });     
        $scope.videoClick = function()
        {           
            $log.info('received.');
            $uibModal.open(
            {
                templateUrl: '/views/content.html',

            });
        };          
    });

userspace.html

   <li ng-repeat="list in lists" ng-click="videoClick(list.src)">{{list.name}}</li>

Change your function to accept a parameter

$scope.videoClick = function(val) {
    $log.info('received.');
    $uibModal.open({
        templateUrl: '/views/content.html',

    });
};

Or change the function in HTML

<li ng-repeat="list in lists" ng-click="videoClick()">{{list.name}}</li>

It's because in your;

$scope.videoClick = function()
    {           
        $log.info('received.');
        $uibModal.open(
        {
            templateUrl: '/views/content.html',

        });
    }; 

You have a comma which isn't needed, which makes the function throw an error because it's expecting another property.

Also if you're using the variable you pass through your HTML, you should use Sajeetharan's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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