2014-01-15 43 views
0

我一直在为此挣扎一天,似乎无法找出最佳方式来实现它。JQM&Angular:从页面到页面传递价值

我正在构建Phonegap,Jquery Mobile和Angular JS应用程序。用于页面的JQM,AngularJS从API中提取数据。

我有链接到完整描述的页面的UL列表。点击后,我需要加载另一个JQM页面并将值传递给该页面控制器以启用对API的查询。

我尝试了一切从本地存储,ng-点击工厂分享价值。我似乎无法让第二个控制器看到通过的值。任何人有一个想法,我哪里出错了?

在此先感谢。

列表 - 的的页面控制器

这是加载页面标题的列表视图

// Factory to get categories 
app.factory("api_get_categories_by_group", function ($resource) { 
return $resource(
    "http://MYDOMAIN.com/api/get_categories_by_group", 
    { 
     "group_id": "9" 
    } 
); 
}); 

// Get the list from the factory and put data into $scope.categories so it can be repeated 
function categoryList ($scope, api_get_categories_by_group, getID) { 
    $scope.categories = []; 

    // Get all categories returned by the API 
    $scope.categories = api_get_categories_by_group.query(); 

    $scope.getCatID = function (id) { 
    getID.prepForBroadcast(id); 
    }; 
} 

厂来处理这个控制器是基于数据

共享控制器: http://onehungrymind.com/angularjs-communicating-between-controllers/

app.factory("getID", function ($rootScope) { 
var sharedID = {}; 
sharedID.theID = ''; 

sharedID.prepForBroadcast = function (id) { 
    this.theID = id; 
    this.broadcastItem(); 
}; 

sharedID.broadcastItem = function() { 
    $rootScope.$broadcast('handleBroadcast'); 
}; 

return sharedID; 
}); 

detail-page controller

该控制器需要共享的价值,使API的查询

// Factory to get products by category 
app.factory("api_get_channel_entries_products", function ($resource) { 
return $resource(
    "http://MYDOMAIN.com/feeds/app_productlist/:cat_id", 
    { 
     cat_id:'12' 
    } 
); 
}); 

// Get the list from the factory and put data into $scope.categories so it can be repeated 
function productList ($scope, api_get_channel_entries_products) { 

$scope.products_list = []; 

$scope.$on('handleBroadcast', function() { 
    $scope.catID = getID.sharedID; 
    console.log($scope.catID); 
}); 

$scope.products_list = api_get_channel_entries_products.query(); 
} 

我一般都用实验毫克单击通过传递一个页面的ID - 不过,我还没有将其添加到代码中,因为我认为这可能是问题的根源。

HTML:HG-点击

<li ng-repeat="cat in categories"><a href="#product-list" ng-click="getCatID(cat.cat_id)">{{cat.cat_name}}</a></li> 
+0

哪里试图访问共享数据。你能突出显示那部分代码吗? – Chandermani

+0

糟糕。对不起Chandermani - 这将是有道理的!一般来说,我一直在玩一个hg-click和function。我已经添加到Q.我没有添加该代码,因为我认为这是问题的根源。 – T2theC

+0

'getCatID'方法有些腥意。由于您已经在表达式中使用它,因此在每个摘要周期中这将是主叫方的时间数,并因此一次又一次地广播该消息。 – Chandermani

回答

0

我绝不是专家,但我也有类似的问题,当我想通过控制器之间的数据。下面的方法工作最适合我:

app.service('dataService', function() { 

    var _currentData = {}; 

    return { 
     getCurrentData: function(){ 
      return _currentData; 
     }, 
     setCurrentData: function(input){ 
      _currentData = input; 
     } 
    }; 
}); 

在控制器,将需要访问数据:

$scope.$watch(function() { 
     return dataService.getCurrentData(); 
    }, function(newValue, oldValue) { 
     if (newValue != null) { 
      $scope.Data = newValue; 
     } 
}, true); 
+0

谢谢io_berlin。我会放弃这一点。 – T2theC