1

所以我正在完成一个 AngularJS 教程,所以我知道这不是完成这些任务的最佳方式,但这是我要迈出的一步。我正在尝试将数据加载到视图中,但数据不显示。我的 ng-view 和我的 ng-route 工作正常,可以让我到达那里,但是一旦我在 Patient.html 上,我只有我的表头,没有任何信息。我错过了什么?

这是我的模块

(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>
4

3 回答 3

1

您非常小心地将 roomId 参数转换为 int,然后将其与字符串进行三等式比较。

于 2015-01-28T20:55:40.647 回答
1

我为你完成了你的plunker。

但不确定这是否是您想要实现的目标。

在这里你可以看到它:

笨蛋

<tr >
        <td>{{ patient.first }}</td>
        <td>{{ patient.last }}</td>
</tr>

加上一些链接和路径

于 2015-01-28T23:00:26.723 回答
0

这是您的代码的工作 plunker....稍作修改: 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]);
}());
于 2015-01-29T03:00:08.980 回答