2

I am trying to learn routing by adding routing to a specific page in my rails app using angular which will replace ajax. Below is the console error am getting.

Uncaught Error: [$injector:modulerr] Failed to instantiate module testApp due to: TypeError: Cannot read property 'state' of undefined

This is how I defined.

app.js

app = angular.module("testApp", ['ui.router']);

app.config([
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('mylistings', {
                url: '/mylistings',
                templateUrl: '/views/mylistings.html',
                controller: 'dashboardCtrl'
            });
    }
]);

app.controller('dashboardCtrl', function ($scope, $http, $stateParams) {
$scope.testing="testdata"
});

mylistings.html

{{testing}}

In this case http://localhost:3000/dashboard is the url I want routing. Could someone tell me what am doing wrong here.

4
  • 1
    no url /dashboard shown in config. Commented Jan 30, 2016 at 3:34
  • @charlietfl I want it to be something like localhost:3000/dashboard#mylistings... Commented Jan 30, 2016 at 3:36
  • localhost:3000/dashboard is from my rails app and not by angular. I want routing here because I want a different section localhost:3000/dashboard#mybookings and some others Commented Jan 30, 2016 at 3:38
  • 2
    should have a default set using $urlRouterProvider.otherwise('/'); and a state that matches that url. Right now if you go to http://localhost:3000/dashboard then router doesn't know what to do Commented Jan 30, 2016 at 3:47

1 Answer 1

1

Problem is with definition of config function. If you use array app.config([]) that means that you write safe code for the dependencies prepared for minification. Syntax is:

app.config(['$arg1', '$arg2', function ($arg1, $arg2) {
   }]);

So when code is minified you will get something like:

app.config(['$arg1', '$arg2',function(a,b) {}]);

In your example in app.js change config to following code if you want to write code safe for minification (if you are not using ng-annotate):

app = angular.module("testApp", ['ui.router']);

app.config(['$stateProvider','$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('mylistings', {
                url: '/mylistings',
                templateUrl: '/views/mylistings.html',
                controller: 'dashboardCtrl'
            });
    }
]);

app.controller('dashboardCtrl', function ($scope, $http, $stateParams) {
  $scope.testing="testdata"
});

or you can remove [] from config function :

app.config(function ($stateProvider, $urlRouterProvider) {
 ...
}):
Sign up to request clarification or add additional context in comments.

1 Comment

Based on the error that he gets 'Cannot read property 'state' of undefined', that's the problem. Just remove [] from config function definition. Regarding url problem, that's second problem based on configuration, but that is not the question asked about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.