javascript - $scope-less Controller 从 webapi 检索数据不会更新 AngularJS 1.3 中的绑定(bind)

标签 javascript angularjs asp.net-web-api angularjs-scope

好的,阅读下面的链接,在 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/

相关文章:

javascript - ng-repeat $index 到字母表中的字符

javascript - Angular promise : get data from buffer if available

javascript - 将复杂对象从 Angular JS 传递到 Web api 它总是返回 404

javascript - 下载多个SFTP文件时NodeJS报错 "Possible EventEmitter memory leak detected. 11 error listeners added"

javascript - 在 try catch 操作期间*究竟*发生了什么?

JavaScript 依赖管理 : npm vs. bower 与 volo

json - 如何使用 Angular JS 绑定(bind)从 @symbol 开始的 JSON 键

c# - 如何在 web api 中重建返回类型对象

javascript - 如何在 web api mvc 元素中使用显示而不是可见性?

JavaScript 如何从作为参数传递的对象访问变量?