尝试返回已填充的数组,但它返回一个空表

时间:2016-05-26 12:40:40

标签: angularjs factory

.controller("TasksCtrl", function ($scope, tasksFactory) {

            $scope.tasks = tasks;

            $scope.addNew = function(task){
              alert("Add new called!");
              $scope.tasks.push(task);
            };

            $scope.remove = function(index){

              $scope.tasks.splice(index, 1);
            };
            $scope.edit= function(task){
              $scope.current = task;
            };
            $scope.save= function(task){
              $scope.current = {};
            };

        })

所以我不知道这个错误是在工厂还是在我的控制器中

        **.factory('tasksFactory', function(){
          var tasks = [
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
            ];
            return tasks;

2 个答案:

答案 0 :(得分:0)

这里的问题是您将$ scope.tasks设置为任务,但任务只是工厂范围内的变量。在您的原始帖子中,最快的答案就是:

$scope.tasks = tasksFactory;

...但是,我强烈建议您使用“getter”模式,因为它更标准。

我的建议是试试这个:

<强>控制器:

.controller("TasksCtrl", function ($scope, tasksFactory) {

            $scope.tasks = tasksFactory.getTasks();

            $scope.addNew = function(task){
              alert("Add new called!");
              $scope.tasks.push(task);
            };

            $scope.remove = function(index){

              $scope.tasks.splice(index, 1);
            };
            $scope.edit= function(task){
              $scope.current = task;
            };
            $scope.save= function(task){
              $scope.current = {};
            };

})

<强>工厂

.factory('tasksFactory', function(){
          var tasks = [
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
                { info: "Finish with the sales report for July", date:Date("October 13 ,2014 11:13:00"), hoursplanned:140, hoursworked:166 },
            ];

    function getTasks() {
        return tasks;
    }

}

答案 1 :(得分:0)

tasksFactory返回一个数组。如果您真的想要使用它,请尝试这个小实验 - 而不是tasks调用工厂Enter the name for this agent (default is Agent-WPTFS05) VS2015U2 Enter the URL for the Team Foundation Server (default is ) http://tfs:8080/tfs Configure this agent against which agent pool? (default pool name is 'default') VS2015U2 Enter the path of the work folder for this agent (default is 'C:\Users\svc_tfsbuild\Agent\_work') Would you like to install the agent as a Windows Service (Y/N) (default is N) Y Enter the name of the user account to use for the service (default is NT AUTHORITY\NETWORK SERVICE) svc_tfsService Enter the password for user account svc_tfsService: TF246018: The database operation exceeded the timeout limit and has been cancelled. Verify that the parameters of the operation are correct. 。让我知道会发生什么。