简体   繁体   English

为什么这个AngularJS ng-repeat示例不起作用?

[英]Why is this AngularJS ng-repeat example not working?

Sorry for asking this. 抱歉问这个。 But I searched everywhere and I can't understand why this example is not working: 但是我到处搜索了,我不明白为什么这个例子不起作用:

HTML: HTML:

<div ng-controller="VenuesController">
            <div ng-repeat="field in fields">
                <!-- Table -->
                <table class="table">
                    <tr>
                        <td><p align=center>{{field.name}} - {{field.sport}} {{field.price}}
                        </p></td>
                        <td><p align=center></p></td>
                    </tr>
                    <tr ng-repeat="hour in field.hours">
                        <td><p align=center>{{hour.from}} - {{hour.to}}</p></td>
                        <td><p align=center><button type="button" class="btn btn-success">{{hour.state}}</button></p></td>
                    </tr>
                </table>
            </div>
        </div>

JS: JS:

$scope.searchFieldsState = function() {

    $scope.fields = [
        {'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
                ]
        },
        {'name': 'Cancha 2', 'sport': 'Futbol', 'price': '$170',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
            ]
        },
        {'name': 'Cancha 3', 'sport': 'Tenis', 'price': '$170',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
            ]
        }
    ];
};

I'm sure that the function searchFieldsState is being executed because I used a console.log command for debugging purposes. 我确定函数searchFieldsState正在执行,因为我使用console.log命令进行调试。

So, can anyone see any mistake here? 那么,有人在这里看到任何错误吗? A fresh eye would be helpful. 新鲜的眼睛会有所帮助。

Thank you very much in advance. 提前非常感谢您。

UPDATE: 更新:

I initialized the $scope.fields outside the function and ng-repeat worked. 我在函数外部初始化了$ scope.fields,并且ng-repeat起作用了。 But this is not what I need so... The problem persist 但这不是我所需要的...问题仍然存在

I will add my navbar code because I think it is related with this issue: 我将添加导航栏代码,因为我认为它与以下问题有关:

<ul class="nav navbar-nav navbar-right">
        <li><a href="#/sportmap">SportMap</a></li>
        <li><a href="#/lockerroom">Ingresar</a></li>
        <li ><a ng-controller="VenuesController" href="#/venue">Administraci&oacute;n</a></li>
      </ul>

The route: 路线:

myApp.config(['$routeProvider',
function($routeProvider) {
    $routeProvider.
    when('/index', {
        templateUrl: 'search.html',
        controller: 'VenuesController',
        restricted: false
    }).
    when('/venue', {
        templateUrl: 'venue.html',
        controller: 'VenuesController'
    }).
    otherwise({
        redirectTo: '/index'
    });

}]); }]);

Thank you guys! 感谢大伙们!


Updating with the working code: 更新工作代码:

The HTML file with the ng-repeat was ok. 带有ng-repeat的HTML文件还可以。

I had to modify the navbar.html template 我必须修改navbar.html模板

<ul class="nav navbar-nav navbar-right">
        <li><a href="#/sportmap">SportMap</a></li>
        <li><a href="#/lockerroom">Ingresar</a></li>
        <li ><a href="#/venue/1" >Administraci&oacute;n</a></li>
</ul>

So then I changed the route 所以我改变了路线

when('/venue/:venueid', {
   templateUrl: 'venue.html',
   controller: 'VenuesController'
}).

with this I receive a parameter called venueid. 这样,我会收到一个名为会场编号的参数。 And then... 接着...

the magical trick based in Carlos Barcelona suggestion: 巴塞罗那卡洛斯(Carlos Barcelona)建议的魔术技巧:

var venuesControllers = angular.module('myApp');

venuesControllers.controller('VenuesController', ['$scope', '$resource', '$location', '$routeParams',
function ($scope, $resource, $location, $routeParams) {

//This is a venue initialization it may be no needed. TODO: try not to use it.
$scope.venue = {city: "", sport: "", venueid: ""};

//Here I just declare the function

$scope.showVenueFields = function () {
    // To search by city and/or sport
    console.log($scope.venue.venueid);
    $scope.venue.name = 'El Andén';
    $scope.venue.fields = [
        {'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
            ]
        },
        {'name': 'Cancha 2', 'sport': 'Futbol', 'price': '$170',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
            ]
        },
        {'name': 'Cancha 3', 'sport': 'Tenis', 'price': '$170',
            'hours' : [
                {'from': '10:00', 'to': '11:00', 'state': 'Libre'},
                {'from': '11:00', 'to': '12:00', 'state': 'Libre'},
                {'from': '12:00', 'to': '13:00', 'state': 'Libre'},
                {'from': '13:00', 'to': '14:00', 'state': 'Libre'}
            ]
        }
    ];
}


if ($routeParams.venueid){
    console.log("Leo los parametros");
    $scope.venue.venueid = $routeParams.venueid;
    //Here I really call the function and initialize the venue.fields
    $scope.showVenueFields();

} else {
    // Do something
}



// this ends the controller's declaration
}]);

you should change the way the Controller is called 您应该更改Controller的调用方式

instead of this: 代替这个:

$scope.searchFieldsState = function() {
  $scope.fields = [
    {'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
        'hours' : [
 etc...

type this: 输入:

function VenuesController($scope) {
  $scope.fields = [
    {'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
 ... 

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

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