通过变量传递角度对象属性?

时间:2018-07-20 14:18:25

标签: angularjs

控制器:

'use strict';
const fs = require('fs');
const http2 = require('http2');
const koa = require('koa');

const app = new koa();

const options = {
    key: fs.readFileSync('./key.pem'),
    cert: fs.readFileSync('./cert.pem'),
    passphrase: 'test'
};

function getFile(path) {
    const filePath = `${__dirname}/public/${path}`;
    try {
        const content = fs.openSync(filePath, 'r');
        const contentType = 'text/html';
        return {
            content,
            headers: {
                'content-type': contentType
            }
        };
    } catch (e) {
        return null;
    }
}

// response
app.use(ctx => {
    if (ctx.request.url === '/file') {
        const file = getFile('thefile.html');
        ctx.res.stream.respondWithFD(file.content, file.headers);
    } else {
        ctx.body = 'OK' ;
    }
});

const server = http2.createSecureServer(options, app.callback());
console.log('Listening on port 8080');
server.listen(8080);

如何通过“ foo”访问{{item.name}}?

这不起作用:

HTML:

$scope.item = {"name": "b", "code": 3}
$scope.foo = "name";

2 个答案:

答案 0 :(得分:1)

尝试使用此模板HTML:

{{item[foo]}}

答案 1 :(得分:0)

使用方括号将变量的值用作对象的键。您正在将角度表达式{{}}嵌套在另一个无法访问对象键值的表达式中。

angular.module('myApp', []).controller('personCtrl', function($scope) {
   $scope.item = {"name": "b", "code": 3}
   $scope.foo = "name";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="personCtrl">

  <p>{{item[foo]}}</p>

</div>