AngularJS变量未解析/显示
AngularJS variables not resolving/displaying
以下是相关的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";
});
});
现在,一切都按照上面的方式设置好了(其他页面和所有页面都有一些额外的控制器(,controllerMain的消息会在HTML中的指定位置正确显示:"This is the Main Controller"。
现在,我需要在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}}"的地方没有显示任何内容。经过一点调试,我了解到,基本上,如果进行$httpGET调用,则不会显示任何内容。如果我取消呼叫,则会显示消息。
这里有什么问题?这与程序的配置方式有关吗?我是不是不了解控制器的工作方式?
请尝试删除您不需要从控制器返回任何东西的返回
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显示变量”
- Cartodb信息窗口未在地图上显示变量
- 如何更新和显示变量
- 如果项目不匹配,则显示变量
- 在 HTML 中显示变量的好做法是什么?
- 在 Javascript 中,值不来,而是显示变量名称
- 缩短角度显示变量
- 如何使用Tinybox显示变量
- 在不同条件下显示变量的不同值
- console.log() 在变量值实际更改之前显示变量的更改值
- 引导程序引导盒在消息中显示变量
- 仅显示变量内的一段文本
- 我想使用 innerHTML 显示变量的值,但我的代码不起作用
- 使用带有按钮的JS函数来显示变量
- 无法使用车把正确在我的“模板”中显示变量.js
- 选中选择框选项时显示变量
- 如何使用显示变量url的iframe打开引导模式
- 我想显示变量'b[i]'而不是'x[i].childNodes[0].nodeValue'
- 数字框jeasyui没有显示变量中的值
- 如何在AngularJS网站中显示变量的名称