简体   繁体   中英

AngularJS ng-init not working with ng-repeat

I've been trying to call passID() function when page initializes but the function still doesn't work.

The website works this way: Page-A passes data to Controller-A then to Service then to Controller-B then the function from Controller-B gets called by Page-B .

My guess is that my error is in Page-B since I've successfully passed the data of Page-A up to Controller-B but I'm not 100% sure.

In my Page-A , I've made a button calling the function of Controller-A using ng-Click

<a href="#/compare-details"><button class="viewDetails" ng-click="passID([1,03,07])">test button</button></a>

Controller-A only gets data from Page-A

angular.module('handsetExplorerModule')
.controller("compareHandsetController", ['$scope','compareHandsetService','sharedData', function($scope, compareHandsetService, sharedData) {


    $scope.passID = function(id){
        var arrayID = [];
        arrayID = id;
        sharedData.set(arrayID);
    };

}]);

Service gets my JSON and is my getter and setter

angular.module('handsetExplorerModule')
.factory('compareDetailsService', ['$http','$q', function($http, $q) {
  return {
    getHandsetList: function(){
      return $http.get("./data/compareJSON.json");
    }
  };
}]);


angular.module('handsetExplorerModule')
.factory('sharedData', function() {
 var savedData = {}
 function set(data) {
   savedData = data;
 }
 function get() {
  return savedData;
 }

 return {
  set: set,
  get: get
 }

});

Controller-B filters the JSON file to only get what I need (i used ID from Page-A to filter)

angular.module('handsetExplorerModule')
.controller("compareDetailsController", ['$scope','compareDetailsService','sharedData', function($scope, compareDetailsService, sharedData) {

     $scope.data = {phones: []};
  compareDetailsService.getHandsetList()
   .then(
       function(data){
         $scope.data.phones = data.data;
       }, 
       function(error){
           //todo: handle error
       }
    );

    var getData = sharedData.get();

    $scope.passID = function passID(){
            var newData = [];
            for(var i=0; i<$scope.data.phones.length; i++){
                if($scope.data.phones[i].id == getData[0] || $scope.data.phones[i].id == getData[01] || $scope.data.phones[i].id == getData[02]){
                    newData.push($scope.data.phones[i]);
                }   
            }
            $scope.phoneContent = newData;

        } 

   $scope.viewDetails = function(id){
        alert(id);
    } 

}]);

Lastly, In my Page-B , I've called Controller-B function: <div ng-init="passID()">

to be used by ng-repeat of my Page-B :

<table id="Content"  ng-repeat="x in phoneContent track by x.id">
            <tr>
                <td class="one">
                    <table>
                    <tr>
                        <td><img class="contImage" ng-src="{{x.image}}" ng-alt="{{x.name}}" /></td>
                        <td class="textAlign">{{x.name}} <button class="viewDetails" ng-click="viewDetails(x.id)">VIEW DETAILS</button></td>
                    </table>
                </td>       
                <td class="two">{{x.size}}</td>
                <td class="one">{{x.storage}}</td>
                <td class="two">{{x.camera}}</td>
                <td class="one">{{x.battery}}</td>
                <td class="two">{{x.network}}</td>
                <td class="one">{{x.sim}}</td>
            </tr>
            </table>

I don't know why you define arrayID . To solve your problem try with sharedData.set = arrayID; instead of sharedData.set(arrayID); .

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM