2016-05-26 8 views
-1
.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 = {}; 
      }; 

     }) 

Ich weiß also nicht, ob der Fehler in der Fabrik oder in meinem Controller istversuchen, ein Array bereits ausgefüllt zurück, aber es gibt eine leere Tabelle

 **.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; 
+0

Was ist die Frage? – jlew

Antwort

0

Das Problem hier ist, Sie stellen $ scope.Tasks zu Aufgaben, aber Aufgaben sind nur eine Variable innerhalb des Umfangs der Fabrik. In Ihrer ursprünglichen Post würde die schnellste Antwort nur sein:

$scope.tasks = tasksFactory; 

... aber ich empfehle Sie ein „Getter“ Muster verwenden, wie es Standard ist.

Mein Vorschlag ist, dies zu versuchen:

Controller:

.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 = {}; 
      }; 

}) 

Fabrik

.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; 
    } 

} 
0

TasksFactory ein Array zurückgibt. Wenn Sie es wirklich verwenden möchten, versuchen Sie dieses kleine Experiment - anstelle von tasksFactory rufen Sie die Fabrik tasks. Lass mich wissen was passiert.