首页 > 解决方案 > 使用 $ctrl 而不是 $scope 时在 AngularJS 中显示 SQL 查询的结果

问题描述

所以我在 AngularJS 中显示 SQL 查询的结果时遇到问题。

PHP文件完美运行

模板

<table id="penetration">
   <tr>
       <th>Penetration Pipe 33.1</th>
       <th>Penetration Pipe 33.2</th>
   </tr>
   <tr>
       <td>TT0101: <span class="value"></span><br/>
           TT0102: <br/>
            {{ $ctrl.TT0101Value }}
            TT0103: </td>
       <td>TT0107: </td>
    </tr>
</table>

零件

'use strict';
angular.module('cryostat', []).component('cryostat', {
    templateUrl: 'cryostat/cryostat.template.html',
    controller: function cryostatController($scope, $http, $q) {
        this.pageTitle = "NP04 Cryostat"
        this.natalie = 1;
        var temp1 = $http.get("cryostat/cryostat.conn.php");
        var temp2 = $http.get("cryostat/cryostat.conn.php");
        $q.all([temp1, temp2]).then(function (resultArray) {
            cryostatController.TT0101 = resultArray[0].data.records;
            cryostatController.TT0101Value = cryostatController.TT0101[0].Mnish;
            console.log(cryostatController.TT0101Value);
            console.log(cryostatController.TT0101[0].Mnish);
            // this.TT0102 = resultArray[1];
    });
    this.httpresult = $http.get("cryostat/cryostat.conn.php")
        .then(function (response) {cryostatController.resss = response.data.records;});
}
});

问题是它是空的,即不显示任何内容,也尝试使用 this.TT0101 但这会引发错误未定义

问题是什么?

标签: htmlangularjs

解决方案


刮那个。自己找到了答案。

self替换了cryostatController并添加了var self = this;

'use strict';
angular.module('cryostat', []).component('cryostat', {
    templateUrl: 'cryostat/cryostat.template.html',
    controller: function cryostatController($scope, $http, $q) {
        this.pageTitle = "NP04 Cryostat"
        this.natalie = 1;
        var self = this;
        var temp1 = $http.get("cryostat/cryostat.conn.php");
        var temp2 = $http.get("cryostat/cryostat.conn.php");
        $q.all([temp1, temp2]).then(function (resultArray) {
            self.TT0101 = resultArray[0].data.records;
            // this.TT0102 = resultArray[1];
    });
}
});

推荐阅读