好的,阅读下面的链接,在 Angular 1.3 中,他们建议不要在 Controller 内使用“$scope.x”,而是使用“this.x”
https://docs.angularjs.org/api/ng/directive/ngController
似乎中断的是调用 $http 来获取数据(本质上是异步的),但当 2.4 秒的 webapi 调用发生时,绑定(bind)不会更新。
我可以看到进行了 webapi 调用并返回数据,但数据没有更新到页面。
我看到的每个示例都使用 $scope 使用模式。我正在尝试在有时间的情况下对我的代码进行 future 验证。
这是从服务返回的 JSON
[{"Venue":"Index","Symbol":"ADDA.IDX","SecurityId":3320,"Type":"Index","Description":"AMEX Advance Decline Difference"},
{"Venue":"Index","Symbol":"ADDD.IDX","SecurityId":3321,"Type":"Index","Description":"OTC Advance Decline Difference"},
...
这是标记
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<body>
<h1>My Test</h1>
<div id="t" ng-controller="SecuritiesController as venue">
<ul>
<li ng-repeat="s in venue.securities">
{{s.SecurityId}} {{s.Symbol}} {{s.Description}}
</li>
</ul>
</div>
<script src="scripts/angular-1.3.js"></script>
<script src="app/controllers/securityController.js"></script>
</body>
</html>
这是 Controller
angular.module('myApp', []).controller('SecuritiesController', function ($http) {
$http.get("/api/securities").success(function(results)
{ this.securities = results.data; }
);
});
最佳答案
将代码更改为
angular.module('myApp', []).controller('SecuritiesController', function ($http) {
var controller = this;
$http.get("/api/securities").success(function(results)
{ controller.securities = results.data; }
);
});
在响应函数中,这实际上指的是 Closure block 而不是 Controller 。
问题作者:删除 .data 并且它可以工作 - 我的错误!
关于javascript - $scope-less Controller 从 webapi 检索数据不会更新 AngularJS 1.3 中的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577794/