javascript - 数据不会在 Angular View 中加载

标签 javascript angularjs ng-view

所以我正在学习 AngularJS 教程,所以我知道这不是完成这些任务的最佳方式,但这是我正在进行的步骤。我正在尝试将数据加载到 View 中,但数据不显示。我的 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 : '/' });
    });

}());

这是我的 Controller

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

最佳答案

我为你完成了你的 plunker。

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

在这里你可以看到它:

plunker

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

加上一些链接和路径

关于javascript - 数据不会在 Angular View 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28202047/

相关文章:

javascript - 使用 PHP 将带有 < 和 > 运算符的字符串插入数据库

java - 尝试从 URL 打开 PDF 文件时,浏览器上会显示一些二进制数据

javascript - 是否使用 Promise 或通过 $http 调用延迟

javascript - Angular UI网格无法对具有行索引的列进行排序

javascript - Angular ngView 不显示页面

javascript - 使用 ng-view 时可以保留变量作为 View 之间的引用吗?

javascript - 如何捕获输入元素的内容并测试它是否是字母 a-z A-Z 之间的字符,并且两个单词之间有空格

javascript - D3js 重置拖动行为的起源

javascript - 使用 ng-view 并按需加载 Controller

javascript - 正则表达式中的多个条件 - 如何用破折号替换冒号和空格