我得到一个TypeError:undefined不是一个函数。当使用Angular ..
我按照这个顺序加载脚本:
_Layout.cshtml
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular-resource.js"></script>
<script src="~/Scripts/angular-route.js"></script>
<script src="~/Scripts/toastr.js"></script>
<script src="~/Scripts/App/app.js"></script>
<script src="~/Scripts/Authentication/authenticationRepository.js"></script>
<script src="~/Scripts/Authentication/authenticationController.js"></script>
<script src="~/Scripts/Notification/notificationFactory.js"></script>
@RenderSection("scripts", required: false)
app.js
var app = angular.module("seducApp", ['ngRoute', 'ngResource'])
.config(function ($routeProvider, $locationProvider) {
//Login
$routeProvider.when('/User/Login', {
templateUrl: '/templates/User/Login.html'
}).
otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
authenticationController.js
app.controller("authenticationController", function ($scope, authenticationRepository, notificationFactory, $location) {
$scope.login = function (loginVM, returnUrl) {
notificationFactory.error = false;
authenticationRepository.login(loginVM, returnUrl).$promise.then(
function () { $location.url('Home'); },
function () { notificationFactory.error('Fejl i login', 'Fejl'); });
};
$scope.master = {};
$scope.reset = function () {
$scope.loginVM = angular.copy($scope.master);
};
$scope.isUnchanged = function (loginVM) {
return angular.equals(loginVM, $scope.master);
};
$scope.reset();
});
authenticationRepository.js
'use strict';
app.factory('authenticationRepository', function ($resource) {
return {
login: function (loignVM, returnUrl) {
return $resource('/api/User').Login(loignVM, returnUrl);
}
};
});
我是Angularjs的新手,我可以找到问题..
答案 0 :(得分:1)
您正在呼叫$resource('/api/User').Login(...)
,但未定义Login()
方法
如果您希望资源类具有特殊/自定义操作(即方法),则需要在创建时指定(以及必要的属性(例如方法,参数等))。
(的 More info here 强>)。
app.factory('authenticationRepository', function ($resource) {
var User = $resource('/api/User', {
Login: {
method: ...,
[headers: ...,]
[params: ...,]
[data: ...,]
...
}
});
return {
login: function (loignVM, returnUrl) {
return User.Login(loignVM, returnUrl);
}
};
});
答案 1 :(得分:0)
我的工作方式如下:
'use strict';
app.factory('authenticationRepository', function ($resource) {
return {
login: function (model) {
return $resource('/api/User/Login').save(model);
}
};
});
'use strict';
app.controller("authenticationController", function ($scope, authenticationRepository, $location, notificationFactory) {
$scope.login = function (model) {
notificationFactory.error = false;
authenticationRepository.login(model).$promise.then(
function () { $location.url('/Home/Index');},
function (response) { notificationFactory.error = response.data; });
};
$scope.master = {};
$scope.reset = function () {
$scope.model = angular.copy($scope.master);
};
$scope.isUnchanged = function (model) {
return angular.equals(model, $scope.master);
};
$scope.reset();
});