数据不会在Angular视图中加载

时间:2015-01-28 20:43:24

标签: javascript angularjs ng-view

所以我正在学习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>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我为你完成了你的plunker。

但不确定这是否是你想要的。

在这里你可以看到它:

plunker

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

加上一些链接和路径

答案 2 :(得分:0)

以下是您的代码的工作内容....稍作修改: 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]);
}());