gpt4 book ai didi

javascript - AngularJS 变量无法解析/显示

转载 作者:行者123 更新时间:2023-12-03 12:12:59 24 4
gpt4 key购买 nike

以下是相关的 HTML:

<!DOCTYPE html>
<html ng-app="wpApp">

<body>
<div ng-controller="controllerMain as main">
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-left">
<li><a href="#">Link 1</a></li>
<li><a href="#two">Link 2</a></li>
<li><a href="#three">Link 3</a></li>
</ul>
</div>

<!-- Start of View State -->
<div class="page-content" style="padding-left:10px; padding-right:10px">
<div ng-view></div>
</div>
<!-- End of View State -->

<div class="footer">
<div class="banner">
{{main.message}}
</div>
</div>
</div>
</body>
</html>

这是相关的 Javascript

wpApp = angular.module("wpApp", ["ngRoute", "ngAnimate", "ui.bootstrap", "angularFileUpload"]);

wpApp.config(function($routeProvider) {
return $routeProvider.when("/", {
templateUrl: "one.html",
controller: "controllerOne"
}).when("/two", {
templateUrl: "two.html",
controller: "controllerTwo"
}).when("/three", {
templateUrl: "three.html",
controller: "controllerThree"
});
});

wpApp.controller("controllerMain", function() {
this.ready = 'true';
this.message = "This is the Main Controller";
});
});

现在,所有内容都按照上面的方式完全设置(为其他页面和所有页面添加了一些附加 Controller ),controllerMain 的消息将正确显示:“这是主 Controller ”在 HTML 中的指定位置。

现在,我需要在controllerMain中进行$http调用,所以我像这样更改它:

wpApp.controller("controllerMain", function($http) {
this.ready = 'true';
this.message = "This is the Main Controller";
return $http.get("config.json").success(function(data) {
console.log("Here");
});
});

发生这种情况时,我可以在日志中看到“Here”,但应显示“{{main.message}}”的地方没有显示任何内容。一些调试表明,基本上如果进行 $http GET 调用,则不会显示任何内容。如果我删除通话,则会显示该消息。

这里有什么问题吗?与程序的配置方式有关吗?我是否不了解 Controller 的工作方式?

最佳答案

请尝试删除您不需要从 Controller 返回任何内容的返回

wpApp.controller("controllerMain", function($scope, $http) {
$scope.ready = 'true';
$scope.message = "This is the Main Controller";
$http.get("config.json").success(function(data) {
console.log("Here");
});
});

我还建议使用 $scope.message = "This is the Main Controller";以及 html {{message}}

关于javascript - AngularJS 变量无法解析/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872044/

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