Angular 视图中不会加载数据
Data won't load in Angular view
所以我正在学习 AngularJS 教程,所以我知道这不是完成这些任务的最佳方法,但这是我正在进行的步骤。我正在尝试将数据加载到视图中,但数据不显示。我的 ng-view 和我的 ng-route 可以正常工作让我到达那里,但是一旦我进入 patient.html,我只有我的 table headers,没有信息。我错过了什么?
这是我的模块
(function() {
var app = angular.module('cbApp', ['ngRoute']);
app.config(function($routeProvider){
$routeProvider
.when('/',{
controller : 'MainCtrl',
templateUrl : 'views/main.html'
})
.when('/patient/:roomId', {
controller : 'PatientCtrl',
templateUrl : 'views/patient.html'
})
.otherwise({ redirectTo : '/' });
});
}());
这是我的控制器
(function() {
var PatientCtrl = function ($scope, $routeParams) {
var roomId = $routeParams.roomId;
$scope.patient = null;
//create a function that searches the rooms for id
function init() {
var length = $scope.rooms.length;
for (var i = 0; i < length; i++){
if ($scope.rooms[i].name === parseInt(roomId)) {
$scope.patient = $scope.rooms[i].patient;
break;
}
}
}
$scope.rooms = [
{ id : 101, name : "101", patient : { id : 1, last: "Ratcliff", first : "Daniel"} },
{ id : 102, name : "102", patient : { id : 2, last: "Gibson", first : "Mel"} },
{ id : 103, name : "103", patient : { id : 3, last: "Fey", first : "Tina"} }
];
init();
}
//handle DI to avoid minification errors
PatientCtrl.$inject = ['$scope', '$routeParams'];
//define the controller in angular
angular.module('cbApp').controller('PatientCtrl', PatientCtrl);
}());
和我的观点
<h2>Room Details</h2>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="pt in patient">
<td>{{ pt.first }}</td>
<td>{{ pt.last }}</td>
</tr>
</table>
您非常仔细地将roomId参数转换为int,然后将其与字符串进行三等比比较。
我为你完成了你的 plunker。
但不确定这是否是您想要实现的。
在这里你可以看到它:
<tr >
<td>{{ patient.first }}</td>
<td>{{ patient.last }}</td>
</tr>
加上一些链接和路径
这是您的代码的工作插件....稍作修改:
plunker
angular.module('cbApp', ['ngRoute']).
config(function ($routeProvider) {
//configure your routes
});
(function() {
var PatientCtrl = function ($scope, $routeParams) {
var roomId = $routeParams.roomId || 101;
$scope.patients = [];
//create a function that searches the rooms for id
function init() {
var length = $scope.rooms.length;
for (var i = 0; i < length; i++){
if (parseInt($scope.rooms[i].name) === parseInt(roomId)) {
$scope.patients.push($scope.rooms[i].patient);
break;
}
}
}
$scope.rooms = [
{ id : 101, name : "101", patient : { id : 1, last: "Ratcliff", first : "Daniel"} },
{ id : 102, name : "102", patient : { id : 2, last: "Gibson", first : "Mel"} },
{ id : 103, name : "103", patient : { id : 3, last: "Fey", first : "Tina"} }
];
init();
}
//define the controller in angular
angular.module('cbApp').controller('PatientCtrl', ['$scope', '$routeParams', PatientCtrl]);
}());
所以我正在学习 AngularJS 教程,所以我知道这不是完成这些任务的最佳方法,但这是我正在进行的步骤。我正在尝试将数据加载到视图中,但数据不显示。我的 ng-view 和我的 ng-route 可以正常工作让我到达那里,但是一旦我进入 patient.html,我只有我的 table headers,没有信息。我错过了什么?
这是我的模块
(function() {
var app = angular.module('cbApp', ['ngRoute']);
app.config(function($routeProvider){
$routeProvider
.when('/',{
controller : 'MainCtrl',
templateUrl : 'views/main.html'
})
.when('/patient/:roomId', {
controller : 'PatientCtrl',
templateUrl : 'views/patient.html'
})
.otherwise({ redirectTo : '/' });
});
}());
这是我的控制器
(function() {
var PatientCtrl = function ($scope, $routeParams) {
var roomId = $routeParams.roomId;
$scope.patient = null;
//create a function that searches the rooms for id
function init() {
var length = $scope.rooms.length;
for (var i = 0; i < length; i++){
if ($scope.rooms[i].name === parseInt(roomId)) {
$scope.patient = $scope.rooms[i].patient;
break;
}
}
}
$scope.rooms = [
{ id : 101, name : "101", patient : { id : 1, last: "Ratcliff", first : "Daniel"} },
{ id : 102, name : "102", patient : { id : 2, last: "Gibson", first : "Mel"} },
{ id : 103, name : "103", patient : { id : 3, last: "Fey", first : "Tina"} }
];
init();
}
//handle DI to avoid minification errors
PatientCtrl.$inject = ['$scope', '$routeParams'];
//define the controller in angular
angular.module('cbApp').controller('PatientCtrl', PatientCtrl);
}());
和我的观点
<h2>Room Details</h2>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="pt in patient">
<td>{{ pt.first }}</td>
<td>{{ pt.last }}</td>
</tr>
</table>
您非常仔细地将roomId参数转换为int,然后将其与字符串进行三等比比较。
我为你完成了你的 plunker。
但不确定这是否是您想要实现的。
在这里你可以看到它:
<tr >
<td>{{ patient.first }}</td>
<td>{{ patient.last }}</td>
</tr>
加上一些链接和路径
这是您的代码的工作插件....稍作修改: plunker
angular.module('cbApp', ['ngRoute']).
config(function ($routeProvider) {
//configure your routes
});
(function() {
var PatientCtrl = function ($scope, $routeParams) {
var roomId = $routeParams.roomId || 101;
$scope.patients = [];
//create a function that searches the rooms for id
function init() {
var length = $scope.rooms.length;
for (var i = 0; i < length; i++){
if (parseInt($scope.rooms[i].name) === parseInt(roomId)) {
$scope.patients.push($scope.rooms[i].patient);
break;
}
}
}
$scope.rooms = [
{ id : 101, name : "101", patient : { id : 1, last: "Ratcliff", first : "Daniel"} },
{ id : 102, name : "102", patient : { id : 2, last: "Gibson", first : "Mel"} },
{ id : 103, name : "103", patient : { id : 3, last: "Fey", first : "Tina"} }
];
init();
}
//define the controller in angular
angular.module('cbApp').controller('PatientCtrl', ['$scope', '$routeParams', PatientCtrl]);
}());