工厂和资源使Angular UI路由器无法正常工作

时间:2016-03-31 00:55:14

标签: javascript angularjs node.js

当我插入

时,我一直在关注本教程https://www.youtube.com/watch?v=X_NZr_-RaLw和我的clientapp.js
.factory('UserService', function($resource) {

    return $resource('https://troop.tech/api/users/:user', {user: '@user'});

});

在我的代码中,所有角度UI路由都停止工作。

上下文:

var myApp = angular.module('myApp', ['ui.router','ngRouter'])


myApp.factory('UserService', function($resource) {

    return $resource('https://troop.tech/api/users/:user', {user: '@user'});

});


myApp.config(function($stateProvider, $urlRouterProvider, $httpProvider) {

    $urlRouterProvider.otherwise('/dashboard');

    $stateProvider

        // HOME STATES AND NESTED VIEWS ========================================
        .state('home', {
            url: '/home',
            templateUrl: 'partial-home.html'
        })

        .state('dashboard', {
            url: '/dashboard',
            templateUrl: 'partial-dashboard.html'
        })

        .state('about', {
            url: '/about',
            templateUrl: 'partial-about.html'
        })

        .state('register', {
            url: '/register',
            templateUrl: 'partial-register.html'
        });


    $httpProvider.interceptors.push('authInterceptor');

});


myApp.controller('userController', function ($scope, $http, $window, UserService) {

  $scope.users = UserService.query();
      $scope.setDataForUser = function(userID) {
  };
      $scope.addUser = function(){
  };

    ...

1 个答案:

答案 0 :(得分:0)

在你的factroy中,你使用$ resource作为参数,所以你需要注入内置资源的角度。

在index.html中:

<script src="yourComponentFolder/angular-resource/angular-resource.js"></script>

添加模块ngResource ...

   var myApp = angular.module('myApp', ['ui.router','ngRouter','ngResource']);

Reference in your Application that yoy follow

如果您使用ui-router无需注入ngRouter,那么如果您可以从模块中丢弃ngRouter,则有一件事。