显示TypeError的角度:无法读取未定义的属性“状态" [英] angular showing TypeError: Cannot read property 'state' of undefined
问题描述
我正在尝试通过使用Angular将路由添加到我的rails应用程序中的特定页面来替代Ajax来学习路由.下面是正在收到的控制台错误.
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.
未捕获的错误:[$ injector:modulerr]无法实例化模块testApp由于:TypeError:无法读取未定义的属性状态"
Uncaught Error: [$injector:modulerr] Failed to instantiate module testApp due to: TypeError: Cannot read property 'state' of undefined
这就是我的定义.
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}}
在这种情况下, http://localhost:3000/dashboard
是我要路由的URL.有人可以告诉我这是怎么了.
In this case http://localhost:3000/dashboard
is the url I want routing. Could someone tell me what am doing wrong here.
推荐答案
问题在于配置函数的定义.如果使用数组 app.config([])
,则意味着您为要进行压缩的依赖项编写安全的代码.语法是:
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) {}]);
在您的 app.js 示例中,如果您想编写安全的代码以进行缩小(如果您不使用
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) {
...
}):
这篇关于显示TypeError的角度:无法读取未定义的属性“状态"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!