2014-09-19 36 views
3
var nameSpace = angular.module("MyTutorialApp", []); 

nameSpace.controller("MainController", ['$scope', '$http', 
    function($scope, $http) 
    { 
     $http.get("../api/api.php?fxn=" + encodeURIComponent("getCategories") + 
       "&jsn=" + encodeURIComponent("{'code':'1'}")) 
      .success(function(response) 
      { 
       $scope.names = response; 
      }); 

     $scope.myData = {}; 
     nameSpace.controller("MainController", ['$scope', '$http', 

     $scope.myData.doClick = function($event, name, $scope, $http,$config) 
     { 
      alert(name); 
      var element = name; 
      console.log(element); 
      $http.get("../api/api.php?fxn=" + encodeURIComponent("getSubCategories") + 
        "&jsn=" + encodeURIComponent("{'code':'element'}")) 
       .success(function(response) 
       { 
        $scope.subCat = response; 
       }); 
     }]); 

    } 
]); 

<!DOCTYPE html> 
<head> 
    <title>Learning AngularJS</title> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js" type="text/javascript"></script> 

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> 
    <script src="js/maincontroller.js"></script> 

</head> 
<body ng-app="MyTutorialApp" > 

    <div ng-controller="MainController"> 

<table class="table"> 
    <tr class="row1" ng-repeat="list in names.category"> 
    <td ng-click="myData.doClick($event,list.name)">{{ list.name }}</td> 
    </tr> 
</table> 
</div> 



</body> 
</html> 

嗨,我不能發出第二個http請求,它說get property undefined。我試了很長一段時間,我無法發現哪裏出了問題。請幫助我。我剛開始使用角度。 爲了解釋我試圖實現的目標,第一個http請求調用類別列表,填充列表,然後在點擊任何類別之後,將該類別作爲第二個http請求的jsn發送。它獲取的子類別在單個控制器中的多個Http請求

+0

是否有沒有使用工廠或服務的http調用的具體原因? – 2014-09-19 12:57:15

+0

一點都不,我不知道。它沒有在Angularjs手冊中給出。我不確定如何使用這些 – Cerebus1504 2014-09-19 12:58:18

+0

我會爲你製作一個plunkr,讓你瞭解工廠/服務。現在你可以用$ scope.myData.doClick = function($ event,name)替換$ scope.myData.doClick = function($ event,name,$ scope,$ http,$ config) – 2014-09-19 12:59:54

回答

3

入住這

// Code goes here 
var nameSpace = angular.module("MyTutorialApp", []); 
nameSpace.factory('factoryRefrence', ['$http', '$q', 
    function($http, $q) { 
    return { 
     getCategories: function() { 
     var deferred = $q.defer(); 
     $http.get("../api/api.php?fxn=" + encodeURIComponent("getCategories") + 
      "&jsn=" + encodeURIComponent("{'code':'1'}")) 
      .success(function(response) { 
      deferred.resolve(response); 
      }); 
     return deferred.promise; 
     }, 
     getsubCategories: function(element) { 
     var deferred = $q.defer(); 
     $http.get("../api/api.php?fxn=" + encodeURIComponent("getSubCategories") + 
      "&jsn=" + encodeURIComponent({ 
       'code': element 
      })) 
      .success(function(response) { 
      deferred.resolve(response); 
      }); 
     return deferred.promise; 
     } 
    } 
    } 
]); 
nameSpace.controller("MainController", ['$scope', '$http', 'factoryRefrence', 
    function($scope, $http, factoryRefrence) { 

    factoryRefrence.getCategories().then(function(response) { 
     $scope.names = response; 
    }); 

    $scope.myData = {}; 
    $scope.myData.doClick = function(event, name) { 
     alert(name); 
     var element = name; 
     console.log(element); 
     factoryRefrence.getsubCategories().then(function(response) { 
     $scope.subCat = response; 
     }); 
    } 
    } 
]); 

Demo

這是在工廠功能進行通信的方式。如果你這樣設置,它應該可以正常工作。而且在你的代碼中你定義了兩次控制器,這是不正確的。

+0

TypeError:undefined不是一個函數,這就是當我點擊 – Cerebus1504 2014-09-19 13:33:45

+0

再次檢查我已經更新了plunker – 2014-09-19 13:46:59

+0

真棒它的工作非常感謝 – Cerebus1504 2014-09-19 13:53:03