我是來自Stackoverflow的常規消費者建議。這是一個非常寶貴的工具 - 感謝所有的幫助!如何定義角度方法
但是這是我的第一個問題:我對Angular很陌生,但我一直在爲驢子做服務器端的東西。
我有一個控制器上的方法,它調用服務器REST服務。我的問題是,只要控制器被實例化,就會調用該方法。我知道必須有另一種聲明函數的方式,但我不知道要搜索什麼。
這裏是我的html:
<html lang="en" data-ng-app="mLS" ng-controller="mLSController">
<head>....</head>
...
<li><a ng-if="userName" ng-click="Logout()">logout</a></li>
和我的模塊(模塊DEF是其他地方,但它似乎確定)
var app = angular.module('mLS');
app.controller('mLSController', function ($scope, $http, $window, $location) {
$http({
url: '/api/UI/GetUsername',
method: 'GET'
}).success(function (data, status, headers, config) {
$scope.userName = data;
$scope.desiredPath = $location.path();
if (!$scope.userName && $scope.desiredPath != '/Account/Login')
$window.location.href = '/Account/Login';
});
});
function Logout($http) {
$http({ url: '/api/UI/Logout', method: 'GET' });
}
//app.
// controller('mLSController', function ($scope, $http) {
// $scope.Logout = function() {
// $http({ url: 'api/UI/Logout', method: 'GET' });
// };
// });
//app.
// controller('mLSController', ['$scope', '$http', 'Logout', function ($scope, $http, Logout) {
// $scope.callLogout = function() {
// Logout();
// };
// }]).
// factory('Logout', ['$http', function (protocol) {
// protocol({ url: 'api/UI/Logout', method: 'GET' }).success(function() {
// $scope.Logout = true; });
// }]);
所以我的問題是,當前的代碼:
function Logout($http) {
$http({ url: '/api/UI/Logout', method: 'GET' });
}
只是根本不叫,如果我把它放在控制器上,Logout()
被稱爲co時ntroller被實例化。哪個不理想。
請幫忙!
沒問題隊友:) – Bharat