AngularJS、MVC.Unknown 提供商:$ScopeProvider <- $Scope <- userCtrl
AngularJS, MVC.Unknown provider: $ScopeProvider <- $Scope <- userCtrl
我在尝试从 Web api 加载一些数据并尝试使用 ng-repeat 来了解 Angular 的基础知识时,我的 angularscript 出现错误。
https://docs.angularjs.org/error/$injector/unpr?p0=$ScopeProvider%20%3C-%20$Scope%20%3C-%20userCtrl
我的代码是这样的
我有一个从 _Layout.cshtml 加载的 app.js,它只包含:
angular.module('myApp', []);
然后我的用户-controller.js 也从 _Layout.cshtml 加载,如下所示:
angular.module('myApp')
.controller('userCtrl', ['$Scope', '$http', function ($scope, $http) {
$scope.loading = true;
$http.get('/api/user')
.success(function (data) {
$scope.users = data;
$scope.loading = false;
}).error(function () {
$scope.error = "An error has occured while loading posts!";
$scope.loading = false;
});
}]);
然后这就是我的 HTML 的样子:
@{
ViewBag.Title = "Users";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<html ng-app="myApp" >
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body ng-controller ="userCtrl">
<h1>Users</h1>
<div class="container">
<div ng-repeat="user in userCtrl.users">
First name: {{user.FirstName}}<br />
Last name: {{user.LastName}}<br />
Age: {{user.Age}}<br />
City: {{user.City}}
</div>
</div>
</body>
</html>
bundles.Add(new ScriptBundle("~/bundles/usercontroller").Include(
"~/Scripts/App/user-controller.js"));
bundles.Add(new ScriptBundle("~/bundles/angularjs").Include(
"~/Scripts/angular.min.js",
"~/Scripts/App/app.js"));
然后我在 _Layout 中有这个
@Scripts.Render("~/bundles/angularjs")
@Scripts.Render("~/bundles/usercontroller")
我的问题是,在 ng-repeat 中我有 "user in userCtrl.users" 我将其更改为 "user in users" 因为控制器已经在中定义,现在它似乎正在工作:)
您正在尝试使用大写 "Scope" 注入依赖项,并且您不需要注入 $http 作为依赖项。
如果您的控制器看起来像:
.controller('userCtrl', ['$scope', function ($scope, $http) {
应该修复了。
我在尝试从 Web api 加载一些数据并尝试使用 ng-repeat 来了解 Angular 的基础知识时,我的 angularscript 出现错误。 https://docs.angularjs.org/error/$injector/unpr?p0=$ScopeProvider%20%3C-%20$Scope%20%3C-%20userCtrl
我的代码是这样的
我有一个从 _Layout.cshtml 加载的 app.js,它只包含:
angular.module('myApp', []);
然后我的用户-controller.js 也从 _Layout.cshtml 加载,如下所示:
angular.module('myApp')
.controller('userCtrl', ['$Scope', '$http', function ($scope, $http) {
$scope.loading = true;
$http.get('/api/user')
.success(function (data) {
$scope.users = data;
$scope.loading = false;
}).error(function () {
$scope.error = "An error has occured while loading posts!";
$scope.loading = false;
});
}]);
然后这就是我的 HTML 的样子:
@{
ViewBag.Title = "Users";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<html ng-app="myApp" >
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body ng-controller ="userCtrl">
<h1>Users</h1>
<div class="container">
<div ng-repeat="user in userCtrl.users">
First name: {{user.FirstName}}<br />
Last name: {{user.LastName}}<br />
Age: {{user.Age}}<br />
City: {{user.City}}
</div>
</div>
</body>
</html>
bundles.Add(new ScriptBundle("~/bundles/usercontroller").Include(
"~/Scripts/App/user-controller.js"));
bundles.Add(new ScriptBundle("~/bundles/angularjs").Include(
"~/Scripts/angular.min.js",
"~/Scripts/App/app.js"));
然后我在 _Layout 中有这个
@Scripts.Render("~/bundles/angularjs")
@Scripts.Render("~/bundles/usercontroller")
我的问题是,在 ng-repeat 中我有 "user in userCtrl.users" 我将其更改为 "user in users" 因为控制器已经在中定义,现在它似乎正在工作:)
您正在尝试使用大写 "Scope" 注入依赖项,并且您不需要注入 $http 作为依赖项。
如果您的控制器看起来像:
.controller('userCtrl', ['$scope', function ($scope, $http) {
应该修复了。