在AngularJS中使用$ window或$ location重定向(Using $window or $location to Redirect in AngularJS)
我正在开发的应用程序包含各种状态(使用ui-router),某些状态要求您登录,其他状态可以公开使用。
我已经创建了一个方法,可以有效地检查用户是否登录,但我目前正在遇到的问题实际上是在必要时重定向到我们的登录页面。 应该注意的是,登录页面当前不在AngularJS应用程序中。
app.run(function ($rootScope, $location, $window) { $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if (toState.data.loginReq && !$rootScope.me.loggedIn) { var landingUrl = $window.location.host + "/login"; console.log(landingUrl); $window.open(landingUrl, "_self"); } }); });
console.log正确显示了预期的URL。 之后,我已经尝试从$ window.open到window.location.href几乎所有的一切,无论我试过没有重定向发生。
编辑(已解决):
发现问题。
var landingUrl = $window.location.host + "/login"; $window.open(landingUrl, "_self");
变量landingUrl设置为'domain.com/login',这不适用于$ window.location.href(这是我尝试的一个)。 但是将代码更改为
var landingUrl = "http://" + $window.location.host + "/login"; $window.location.href = landingUrl;
现在工作
The app I am working on contains various states (using ui-router), where some states require you to be logged in, others are publicly available.
I have created a method that validly checks whether a user is logged in, what I am currently having issues with is actually redirecting to our login-page when necessary. It should be noted that the login page is not currently placed within the AngularJS app.
app.run(function ($rootScope, $location, $window) { $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if (toState.data.loginReq && !$rootScope.me.loggedIn) { var landingUrl = $window.location.host + "/login"; console.log(landingUrl); $window.open(landingUrl, "_self"); } }); });
The console.log shows the intended url properly. The line after that, I have tried practically everything from $window.open to window.location.href and no matter what I've tried no redirect happens.
EDIT (RESOLVED):
Found the issue.
var landingUrl = $window.location.host + "/login"; $window.open(landingUrl, "_self");
The variable landingUrl was set to 'domain.com/login', which would not work with $window.location.href (which was one of the things I tried). However after changing the code to
var landingUrl = "http://" + $window.location.host + "/login"; $window.location.href = landingUrl;
it now works.
原文:https://stackoverflow.com/questions/24794115
最满意答案
self.fee
为您提供Fees
模型中的相关模型对象。 因此,您应该访问Fees
模型的fee
字段并进行计算:self.balance = self.fee.fee - self.amount # ^^^
您可以在
Fees
模型中重命名该字段以避免将来进一步混淆;self.fee.amount
肯定读得更好。
self.fee
gives you the related model object from theFees
model. So you should access and do the computation with thefee
field of theFees
model:self.balance = self.fee.fee - self.amount # ^^^
You could rename the field in the
Fees
model to sayamount
to avoid further confusion in the future;self.fee.amount
sure reads better.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
首先,安排你的视图是不行的,虽然它会起作用。 经过验证的表单数据将在form.cleaned_data字典中。 这些数据将很好地转换为Python类型。 诸如IntegerField,DecimalField和FloatField之类的字段分别将值转换为Python int,Decimal和float。 from yourapp.model import Daten def dateneingabe(request): if request.method == "POST": fo ...
-
self.fee为您提供Fees模型中的相关模型对象。 因此,您应该访问Fees模型的fee字段并进行计算: self.balance = self.fee.fee - self.amount # ^^^ 您可以在Fees模型中重命名该字段以避免将来进一步混淆; self.fee.amount肯定读得更好。 self.fee gives you the related model object from the Fees model. So you should ...
-
我看到的第一个错误是def create(self, email, password)应该接收参数self和validated_data 。 例如: def create(self, validated_data): """Create a new model instance""" return MyUser.objects.create(**validated_data) 理想情况下,您的序列化程序将包含需要创建此用户实例的所有字段。 The first error that I se ...
-
您可以使用check命令显式运行检查: python manage.py check 要专门检查模型,您可以执行: python manage.py check --tag models You can explicitly run the checks with the check command: python manage.py check To specifically check the models, you can do: python manage.py check --tag mod ...
-
我已经注释掉清空表格的那一行。 def upload_photos(request, user_name): user = User.objects.get(username=unquote(user_name)) if request.method=='POST': form = PhotoForm(request.POST, request.FILES) if form.is_valid(): forum = form.save(c ...
-
基于2个输入计算(Calculation based on 2 inputs)[2022-03-25]
我建议你首先读取两个输入的值,然后将它们解析为Date对象,然后执行减法(我包括绝对函数来补偿切换值)。 这是一个可能的解决方案: var date_1 = document.getElementById("date_1").value; var date_2 = document.getElementById("date_2").value; var d_1 = Date.parse(date_1); var d_2 = Date.parse(date_2); var diff_milliseconds ... -
Django REST在使用ManyToManyField序列化模型时出错(Django REST Errors when serializing model with ManyToManyField)[2022-06-06]
在你的序列化版本1中,你不必添加 builds = serializers.PrimaryKeyRelatedField() 因为模型序列化程序将为您创建。 事实上,如果你看一下文档的例子( http://www.django-rest-framework.org/api-guide/relations/ ),你会看到当FK'为'时应用PrimaryKeyRelatedField,目前的模型(不是M2M关系)。 我会从序列化程序中删除它,然后看看发生了什么。 In your serializer versi ... -
来自Django Docs 业务逻辑应该进入模型内部。 视图用于定义向用户呈现数据的方式。 “视图”描述了呈现给用户的数据。 它不一定是数据的外观,而是呈现的数据。 该视图描述了您看到的数据,而不是您的看法。 视图应该从业务逻辑中清除,并且应该仅用于使数据在模板上显示/显示,或者换句话说,仅将视图用于视图逻辑。 From the Django Docs Business logic should go inside models. Views are to define the way data is pr ...
-
如何根据django中的用户输入在模型中存储值?(How do I store values in a model based on user input in django?)[2021-11-16]
如果要更新Opening模型,则需要知道哪一个。 将opening_id写入表单。 我不确定你的插槽和开口是如何相关的,所以你必须弄明白这一点。 我通常使用get_object_or_404来获得相关的Opening username = request.POST['username'] opening = get_object_or_404(Opening, id=request.POST['opening_id']) opening.reservation = username opening.save ...