【问题标题】:I need to get products when I clicked on the category list当我点击分类列表时,我需要获取产品
【发布时间】:2016-04-27 19:44:08
【问题描述】:

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

我的类别列表会显示,点击之后,我必须重定向产品页面,当我点击类别时,我会收到类似这样的错误。 “找不到变量:$state”。

CategoryController.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) {

         });

    }]);

app.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: '/'
             });
         });

ProductController.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) {

         });

    }]);

【问题讨论】:

    标签: javascript jquery angularjs angular-ui-router


    【解决方案1】:

    这是一个关于为控制器注入 $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

    state provider and route provider in 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'
             })
    
         });
    

    【讨论】:

    • 现在我喜欢这个错误。无法从状态 '' 解析 '/:id' 你能更改 $stateProvider 的 app.js 文件吗
    • 我根据您的代码部分编辑了我的解释。也许您想查看本教程以获取更多信息:link
    【解决方案2】:

    如果你想使用 $state-Service 你需要先注入它:

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

    【讨论】:

    • 现在我遇到了这个错误。无法从状态 '' 解析 '/:id' 你能更改 $stateProvider 的 app.js 文件吗
    • "/:id" 不是验证状态。对不起,我错过了。 :id 只是不同 id 的占位符,因此您必须明确传递一些 id。更新我的答案
    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2023-01-30
    • 2021-06-09
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多