2017-03-13 33 views
0

这是关于Angular JS One的,所以我在我的plunkr中创建了一个简单的应用程序。 我做了什么? ,我创建了一个名为app.js和myController.js的工厂。AngularJS 1 - 如何将app.js值列表传递给controller.js

问题是来自controller.js的Get方法,我不知道如何正确编写它,因此它会将数据列表传递给html。请检查方法getData(app.js)和Get(controller.js)。有什么错误?

注意:添加和编辑工作正常使用这三个层(app-> controller-> view),我需要。

PS:我什么都没做。

谢谢! 约翰

下面是我的app.js或业务

angular.module('myFirstApp',['ngRoute']) 

.config(function($routeProvider){ 
    $routeProvider 
    .when('/home',{templateUrl : 'home.html'}) 
    .when('/people',{templateUrl : 'people.html'}) 
    .when('/about',{templateUrl : 'about.html'}) 
    .when('/contact',{templateUrl : 'contact.html'}) 

}) 


.factory('personFactory',function(){ 

    function getData(){ 

     $http.get('iphone.json').success(function(result){ 
     return result; 
     }).error(function(error){ 
     alert(error.error + "/" + error.statusCode); 
     }); 

    } 


    function insertData(Name,Email,Password,Mobile){ 

     //update data to database 
     var x = "Record added successfuly."; 
     return x; 
    } 


    function updateData(){ 

     //update data to database 
     var x = "Record updated successfuly."; 
     return x; 
    } 


    return { 
     getData : getData, 
       insertData, 
       updateData 


      }; 


    }) 

下面是我controller.js

angular.module('myFirstApp') 

.controller('myController',function($scope,$http,personFactory){ 


    $scope.Add = function(){ 
    var x = personFactory.insertData($scope.Name,$scope.Email,$scope.Password,$scope.Mobile,$scope.result); 
    $scope.Message = x; 
    return $scope.Message; 
    } 


    $scope.Edit = function(){ 
    var x = personFactory.updateData(); 
    $scope.Message = x; 
    return $scope.Message; 
    } 


    $scope.Get = function(){ 
    var x = personFactory.getData(); 
    $scope.PeopleList = x; 
    return $scope.PeopleList; 
    } 



}) 

下面是我的看法

<html> 
    <head> 

    <title></title> 
    <script src="app.js"></script> 
    </head> 
    <body> 
    <p>People</p> 

    <table ng-controller='myController'> 
     <thead> 
     <tr> 
     <th>Name</th> 
     <th>Email</th> 
     <th>Model</th> 
     <th>Status</th> 
     <th>Purchased on</th> 
     </tr> 
     </thead> 
     <tr ng-repeat="values in PeopleList"> 
     <td>{{values.name}}</td> 
     <td>{{values.email}}</td> 
     <td>{{values.model}}</td> 
     <td>{{values.status}}</td> 
     <td>{{values.purchasedate}}</td> 
     </tr> 
     </table> 
    </body> 
</html> 

回答

1

工厂需要返回一个封装所有功能的JS对象,像这样

// Data factory 
    app.factory("Data", function() { 
     return { 
     add: function() { 
      // add logic here 
     }, 
     get: function() { 
      // get logic here 
     } 
     } 
    } 
0

我想你在工厂内的$http.get('iphone.json')有点问题。也许你应该尝试使用一个回调来处理,就像这样:

app.js

function getData(callback) { 
    $http.get('iphone.json').success(function(result){ 
     callback(result); 
    }).error(function(error){ 
     alert(error.error + "/" + error.statusCode); 
    }); 
} 

controller.js

$scope.Get = function(){ 
    personFactory.getData(function(result) { 
     $scope.PeopleList = result; 
    }); 
} 
+0

接下来会发生什么? –