当我点击类别列表时,我需要获取产品

I need to get products when I clicked on the category list

本文关键字:获取 列表      更新时间:2023-09-26

我正在为这个应用程序使用 Intelxdk。这是一个移动应用程序。

我的类别列表将显示,单击该类别后,我必须重定向产品页面,当我单击类别时,我可能会收到这样的错误。"找不到变量:$state"。

类别控制器.js

app.controller('CategoryController', ['$scope','getServices','JSONDataService', function($scope,getServices,JSONDataService) {
    var url = 'http://look4what.biz/mobile/shop-category.php';
    $scope.getId = function(termid){ 
        JSONDataService.addTansferData(termid);
        $state.go('/:id');
    }
    getServices.sendRequest(url)
        .success(function(data, status, headers, config) {
            $scope.userslists = data;
         }).error(function(data, status, headers, config) {
         });
    }]);

应用.js

var app = angular.module('LookApp', ['ionic','ngCordova','ngRoute','paymentCtrls']);  
    app.config(function($stateProvider, $urlRouterProvider,$routeProvider) {
        $routeProvider 
            .when('/', {
                 controller: 'CategoryController',
                 templateUrl: 'views/category.html'
             })
             .when('/:id', {
                 controller: 'ProductController',
                 templateUrl: 'views/users.html'
             })
             .when('/login/:friendId', {
                  controller: 'LoginController',
                  templateUrl: 'views/login.html'
             })
             .otherwise({
                 redirectTo: '/'
             });
         });

产品控制器.js

app.controller('ProductController', ['$scope', '$routeParams', 'category','JSONDataService','getServices', function($scope, $routeParams, category,JSONDataService,getServices) {
    $scope.newData = JSONDataService.getTansferData();
    term_id="+$scope.newData;
    var url = "http://look4what.biz/mobile/id-get-detail.php?term_id="+termid;
    getServices.sendRequest(url)
        .success(function(data, status, headers, config) {
            $scope.userslists = data;
         })
        .error(function(data, status, headers, config) {
         });
    }]);

下面是有关将$state注入作为控制器服务的小示例:

var app = angular.module('app', ['ui.router']);
app.controller('ctrl', function ($scope, $state) {
  $scope.changeState = function () {
    $state.go('contact.detail');
  };
});

并详细说明:

http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state

AngularJS中的状态提供程序和路由提供程序

您的代码$stateProvider:

app.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/');
    $stateProvider
        .state('home', {
             url:'/'
             controller: 'CategoryController',
             templateUrl: 'views/category.html'
         })
         .state('id', {
              url:'/:id'
             controller: 'ProductController',
             templateUrl: 'views/users.html'
         })
         .state('login.friendId', {
              url:'/login/:friendId'
              controller: 'LoginController',
              templateUrl: 'views/login.html'
         })
     });

如果你想使用$state服务,你需要先注入它:

app.controller('CategoryController', ['$scope','$state','getServices','JSONDataService', function($scope, $state, getServices, JSONDataService) {
    var url = 'http://look4what.biz/mobile/shop-category.php';
    $scope.getId = function(termid){ 
        JSONDataService.addTansferData(termid);
        $state.go('/', { id : termid});
    }
    ...
}