Javascript 为什么这个AngularJS重复示例不起作用?
很抱歉问这个问题。 但是我到处搜索,我不明白为什么这个例子不起作用: HTML: 我确信正在执行函数Javascript 为什么这个AngularJS重复示例不起作用?,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,很抱歉问这个问题。 但是我到处搜索,我不明白为什么这个例子不起作用: HTML: 我确信正在执行函数searchFieldsState,因为我使用了console.log命令进行调试 那么,有人能看出这里有什么错误吗?换一双眼睛会有帮助的 事先非常感谢 更新: 我在函数外部初始化了$scope.fields,并且ng repeat工作了。但这不是我需要的所以。。。问题依然存在 我将添加导航条形码,因为我认为它与此问题有关: <ul class="nav navbar-nav navbar-
searchFieldsState
,因为我使用了console.log
命令进行调试
那么,有人能看出这里有什么错误吗?换一双眼睛会有帮助的
事先非常感谢
更新:
我在函数外部初始化了$scope.fields,并且ng repeat工作了。但这不是我需要的所以。。。问题依然存在
我将添加导航条形码,因为我认为它与此问题有关:
<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ón</a></li>
</ul>
}]))
谢谢你们
使用工作代码更新: 带有ng repeat的HTML文件正常 我不得不修改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ón</a></li>
</ul>
通过这个,我收到一个名为venueid的参数。然后
卡洛斯·巴塞罗那的魔术建议:
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
}]);
您应该更改控制器的调用方式 与此相反:
$scope.searchFieldsState = function() {
$scope.fields = [
{'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
'hours' : [
etc...
键入以下内容:
function VenuesController($scope) {
$scope.fields = [
{'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
...
您是否尝试过在searchFieldsState函数之外定义$scope.fields?Angular可能正在函数内部创建一个本地$scope对象,而您的ng repeat指令正在VenuesController的主作用域中查找“fields”属性(我可能完全错了)。如果对您有利,请不要忘记接受答案。:)@MichaelZalla我试过了,当我这么做的时候,它起作用了。所以你可能是对的。@CarlosBarcelona我会的。但我仍然需要这方面的帮助:-)@Gaborato更新是一个不同的主题,这个主题与ngRouter有关,问题是什么?我看到链接不起作用,但这就是问题所在吗?我建议你打开一个新的问题谢谢你的回答,但它不起作用,但这是一个很好的观察。我会更新我的问题,我相信我到达路线的方式,这是问题的原因。问题是,我一开始使用VenuesController使用index.html模板,因此控制器已初始化。因此,之后,我的链接将我重定向到具有相同控制器的Vince.html模板,该模板将再次初始化控制器,但我编写searchFieldsState函数的方式必须从ng单击中调用它加载页面时它不会运行,为了让ng repeat工作,我需要在作用域中加载这些值,因此我确保该函数在控制器初始化结束时运行。我将用我的代码进行更新tonight@GaboLato很高兴帮助你!我看到你在学习过程中,查看这个网站它真的帮助了我:)谢谢卡洛斯!我来看看
when('/venue/:venueid', {
templateUrl: 'venue.html',
controller: 'VenuesController'
}).
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
}]);
$scope.searchFieldsState = function() {
$scope.fields = [
{'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
'hours' : [
etc...
function VenuesController($scope) {
$scope.fields = [
{'name': 'Cancha 1', 'sport': 'Futbol', 'price': '$150',
...