Javascript 如何在html-Js中调用rootscope函数
我制作了一个Javascript 如何在html-Js中调用rootscope函数,javascript,angularjs,angularjs-rootscope,Javascript,Angularjs,Angularjs Rootscope,我制作了一个rootscope函数,在该函数中,我实际注销了用户。但我不知道如何称呼它在一个观点 我的职能是 $rootScope.logout = function () { $cookies.remove('auth_token'); $cookies.remove('role'); $cookies.remove('status'); $cookies.remove('id');
rootscope
函数,在该函数中,我实际注销了用户。但我不知道如何称呼它在一个观点
我的职能是
$rootScope.logout = function () {
$cookies.remove('auth_token');
$cookies.remove('role');
$cookies.remove('status');
$cookies.remove('id');
$location.path('/signin');
// window.alert("Logout Successfully");
$rootScope.auth_token = $cookies.get('auth_token');
$rootScope.role = $cookies.get('role');
$rootScope.status = $cookies.get('status');
$rootScope.id = $cookies.get('id');
$rootScope.keyword_auth_token = 'Bearer ' + $rootScope.auth_token; //Need to store all of them
$rootScope.is_loggedin = false;
};
我的观点是
<button ng-click="$root.logout()">Logout</button>
注销
我尝试了
$root.logout()
,$rootScope.logout()
,但它不起作用。它应该只在作用域上可用,因为$scope
继承自$rootScope
。尝试:
<button ng-click="logout()">Logout</button>
现在,注销
功能应该可以这样调用:
HTML:
var app = angular.module('myApp', []);
app.run(function($rootScope){
$rootScope.logout = function () {
// ...
}
});
<button ng-click="logout()">Logout</button>
注销
注销
确保使用了正确的控制器,使其在范围内,使用ng submit传递函数。确保您的按钮设置为“提交”。您在哪里注册
$rootScope.logout
函数?在没有控制器的索引文件中,我看到了,我已经更新了我的答案,请让我知道这是否有效。我与@UsmanIqbal有类似的问题,在我的情况下,我在一个隔离的作用域内调用该函数。。。为了解决这个问题,我在HTMLTNX中使用了$root.logout()
,您的答案是我的救星。@MatthewCawley
<form ng-controller="Your controller name" ng-submit="logout()">
<button type="submit">Logout</button>
</form>