【问题标题】:JQM & Angular: Pass value from page to pageJQM 和 Angular:在页面之间传递值
【发布时间】:2014-01-15 14:12:57
【问题描述】:

我已经为此苦苦挣扎了一天,似乎无法找到让它工作的最佳方法。

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

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

我已经尝试了从本地存储到 ng-clicks 工厂以共享值的所有方法。我似乎无法让第二个控制器看到传递的值。有人知道我哪里出错了吗?

提前致谢。

页面列表控制器

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

// 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;
});

详细页面控制器

此控制器需要共享值才能启用 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();
}

我通常一直在尝试使用 mg-click 来传递页面的 id - 但是,我没有将其添加到代码中,因为我认为这可能是问题的根源。

HTML:hg 点击

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

【问题讨论】:

  • 在哪里尝试访问共享数据。你能突出显示那部分代码吗?
  • 糟糕。对不起Chandermani - 那是有道理的!一般来说,我一直在玩 hg-click 和功能。我已添加到 Q。我没有添加该代码,因为我认为这是问题的根源。
  • getCatID 方法有些可疑。由于您已在表达式中使用它,这将是每个摘要周期中的调用者时间,因此一次又一次地广播消息。
  • 啊,这是一个好点。谢谢。您对我可以采取哪些措施来纠正这种情况有什么建议吗?

标签: api angularjs jquery-mobile cordova


【解决方案1】:

我绝不是专家,但是当我想在控制器之间传递数据时,我遇到了类似的问题。以下方法最适合我:

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);

【讨论】:

  • 感谢 io_berlin。我会试一试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2016-04-29
  • 2020-12-07
  • 1970-01-01
相关资源
最近更新 更多