gpt4 book ai didi

javascript - 在 ng-controller 中评估表达式

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:51 26 4
gpt4 key购买 nike

我的Javascript

var app = angular.module('Demo', []);

app.controller('DemoCtrl', function ($scope) {
$scope.expr = "Test"
});
app.controller('Test', function ($scope) {
$scope.HELLO = "HEllo World";
});

我的标记

<body ng-controller="DemoCtrl">
<div ng-controller="{{expr}}">{{HELLO}}</div>
</body>

这不起作用。文档说 ng-controller 可以计算表达式。我做错了什么?

JSBIn Link ( http://jsbin.com/ubevel/13/edit )

最佳答案

将 Controller 编写为函数(而不是 app.controller('Test')

将 JS 更改为:

var app = angular.module('Demo', []);

app.controller('DemoCtrl', function ($scope) {
$scope.expr = Test;
});

function Test($scope) {
$scope.HELLO = "HEllo World";
}

和 HTML:

<body ng-controller="DemoCtrl">
<div ng-controller="expr">{{HELLO}}</div>
</body>

演示 JS BIn

关于javascript - 在 ng-controller 中评估表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com