javascript - 在 angularjs 应用程序中加载初始数据

标签 javascript jquery angularjs

我目前正在尝试开发 AngularJS 应用程序。这是我使用 AngularJS 的第一个应用程序,我想我很清楚它是如何工作的,因为我已经做了几年的 Silverlight 开发人员:-)

但是,有一件简单的事情我想不通:如何在应用程序启动时获取它的初始数据。

我需要的是一个简单的数据表,其中一些字段可以内联编辑(通过下拉菜单)我的应用程序结构是这样的:

app.js

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

feedbackService.js

app.service('feedbackService', function ($http) {
this.getFeedbackPaged = function (nodeId, pageNumber, take) {
    $http.get('myUrl', function (response) {
        return response;
    });
};
});

feedbackController.js

app.controller('feedbackController', function ($scope, feedbackService, $filter) {
// Constructor for this controller
init();

function init() {
    $scope.feedbackItems = feedbackService.getFeedbackPaged(1234, 1, 20);
}
});

标记

<html ng-app="feedbackApp">
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
    <table class="table" style="border: 1px solid #000; width:50%;">
        <tr ng-repeat="fb in feedbackItems | orderBy: 'Id'" style="width:auto !important;">
            <td data-title="Ansvarlig">
                {{ fb.Name }}
            </td>
            <td data-title="Kommentar">
                {{ fb.Comment }}
            </td>
        </tr>
    </table>
</body>

但是当我运行应用程序时表是空的。我认为这是因为应用程序在服务数据添加到 View 模型($scope)之前启动,但我不知道如何在应用程序启动之前对其进行初始化,因此显示了前 20 个表行。

有人知道怎么做吗?

提前致谢!

最佳答案

你应该稍微修改你的代码以使其工作,因为你在这里使用 promise 你应该使用 .then

app.service('feedbackService', function ($http) {
this.getFeedbackPaged = function (nodeId, pageNumber, take) {
    return $http.get('myUrl');
};
});

app.controller('feedbackController', function ($scope, feedbackService, $filter) {
// Constructor for this controller
init();

function init() {
   feedbackService.getFeedbackPaged(1234, 1, 20).then(function(data){$scope.feedbackItems=data;});
}
});

关于javascript - 在 angularjs 应用程序中加载初始数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748115/

相关文章:

document.location.search 更改上的 javascript

php - 基于selectbox ajax php mysql填充文本框

javascript - 通过 ID 获取 CSS 文件以操作属性

javascript - Backbone Jquery Click 在 iOS 上出现巨大延迟

twitter-bootstrap - 无法正确显示 Twitter Bootstrap 字形图标

javascript - 需要/Angular/Karma/ui-router - 错误 : Could not resolve 'landing' from state ''

javascript - TypeError 'undefined' 不是 Javascript/Jquery JSON 解析器中的对象

javascript - 使表头始终可见并始终居中

javascript - 将对象添加到服务中的数组

javascript - 如何限制.exe.png或将.exe文件重命名为png文件以在 Angular 8中上传