如何在不更改URL的情况下在Angular 2应用程序中进行路由?(这是因为应用程序位于Django应用程序页面上的几个选项卡之一下,适合保持URL不变.)
目前我在里面有这样的东西 app.component.ts
@RouteConfig([
{
path: '/home',
name: 'Home',
component: HomeComponent,
useAsDefault: true
},
{
path: '/user/:id',
name: 'UserDetail',
component: UserDetailComponent
}
])
Run Code Online (Sandbox Code Playgroud)
在内部说HomeComponent,导航到用户页面使用以下内容
this._router.navigate(['UserDetail', {id: id}]);
Run Code Online (Sandbox Code Playgroud)
然后网址看起来像 http://localhost:8000/django_url/user/123
当我在Angular 2应用程序中导航时,是否可以保持URL不变?所以http://localhost:8000/django_url当用户在页面上时,网址会保留user/123吗?
谢谢!
我的问题类似于这个问题: 文件()在asp.net mvc中关闭流吗?
我在C#MVC 4中有以下内容.
FileStream fs = new FileStream(pathToFileOnDisk, FileMode.Open);
FileStreamResult fsResult = new FileStreamResult(fs, "Text");
return fsResult;
Run Code Online (Sandbox Code Playgroud)
会fs自动关闭FileStreamResult吗?谢谢!
在Angular 2应用程序中,我希望click事件在按住shift键时触发不同的内容.怎么实现这个?
html如下:
<div class="item" *ngFor="#obj of available" (click)="toggleSelected(obj)"></div>
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
toggleSelected(obj) {
if(shift is pressed) {
do this
} else {
do that
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何检测是否按下了shift键?谢谢!
我需要在django基于类的视图中测试方法和辅助函数.
考虑这个基于类的视图:
class MyClassBasedView(View):
def dispatch(self, request, *args, **kwargs):
....
def __get_render_dict():
d = {}
...
return d
def my_method(self):
render_dict = self.__get_render_dict()
return render_response(self.request, 'template.html', render_dict)
Run Code Online (Sandbox Code Playgroud)
为了为我的视图编写单元测试,我需要调用里面的方法,__get_render_dict()直接说.我怎么能实现这个目标?
我试过了
v = MyClassedBasedView()
v.dispatch(request,args, kwargs)
v.__method_name()
Run Code Online (Sandbox Code Playgroud)
但是在post/get方法中没有匹配的参数失败了,即使我在不使用URL的情况下调用方法direclty.
在Angular 2应用程序中,假设我有一个'SaveComponent',其中单击其保存按钮会调用isDirty()返回true或false 的函数(如果父组件的内容已被修改,则为true).请注意,"SaveComponent"的原因是它具有与之关联的样式和其他"窗口小部件",这些样式在许多组件之间共享.
目前isDirty在每个父组件中定义了一个函数,并且该函数SaveComponent在父模板中传递如下:
<save-component [isDirty]="isDirty"> </save-component>
Run Code Online (Sandbox Code Playgroud)
如果你愿意看,简化版的SaveComponent定义是这样的......
import {Component, Input} from 'angular2/core';
@Component({
selector: 'save-component',
templateUrl: 'who-cares-to-know.component.html'
})
export class SaveComponent {
@Input() isDirty;
}
Run Code Online (Sandbox Code Playgroud)
它最初只起作用.看起来该isDirty函数只返回false(或只调用一次?),即使内容被修改.请注意,从父组件调用该函数时,该函数可以正常工作.
怎么了?是否可以通过@Input或通过其他方式将功能从父组件传递到子组件?谢谢!
在WPF中,这曾经很好用:
<Page.Resources>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
但添加转换器(见下文)会导致第二个资源(Style.xaml)的错误:Each dictionary entry must have an associated key.
<Page.Resources>
<local:MySizeConverter x:Key="sizeConverter"/>
<ResourceDictionary Source="resources/Styles.xaml" />
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
但是,将键添加到第二行(例如<ResourceDictionary x:Key="myStyleDict" Source="resources/Styles.xaml" />,在后面的代码中导致以下错误
The name 'aTextBlockUsedToWork' does not exist in the current context
Run Code Online (Sandbox Code Playgroud)
在那里aTextBlockUsedToWork可以在后面的代码添加键之前成功访问.请注意,如果我注释掉样式资源,转换器工作正常.我怎样才能让这两种资源都有效?
我有一个MergedDictionaries和DateTemplate里面的ResourceDictionary一切,一切都很好,直到我添加一个Converter:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTry">
<local:IsEnabledConverter x:Key="isEnabled"/> <===== causes problem
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="fileinfoTemplate" DataType="{x:Type local:MyFileInfo}">
... template stuff
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
添加该Converter行会导致此错误DataTemplate:
Property elements cannot be in the middle of an element's content. They must be before or after the content.
Run Code Online (Sandbox Code Playgroud)
为什么会导致此错误?
请注意,如果我注释掉,代码编译并且转换器工作正常MergedDictionaries.
[更新]与这个问题的不同之处在于,它不仅要求文本被截断,还要求文本被截断。正如我所提到的,我已经使用这种方法将文本截断了。而是询问仅在文本被截断时才具有“阅读更多”链接(仅在可能的情况下使用CSS)。
目前,我正在使用这种方法在文本溢出时(当文本不能容纳在一行中时)生成省略号。但是,现在我还想在发生溢出时在行的末尾添加一个“阅读更多”链接,单击该链接将在多行中显示所有内容。这仅适用于CSS吗?
顺便说一句,我看到了这篇文章,无论文本是否溢出,它都会显示“更多”链接,这不是我想要的。
我猜想最后一种方法是将JavaScript与resize()侦听器一起使用,以动态隐藏文本的溢出部分并创建链接以显示它们。
谢谢!
我是MVC和C#的新手,很抱歉,如果这个问题看起来太基础了.
对于像下面这样的HttpPost控制器,如何在没有浏览器的情况下直接从用C#编写的客户端程序调用此方法(不是来自带有提交按钮的浏览器中的UI表单)?我使用的是.NET 4和MVC 4.
我确信答案是网上的,但到目前为止还没找到.任何帮助表示赞赏!
[HttpPost]
public Boolean PostDataToDB(int n, string s)
{
//validate and write to database
return false;
}
Run Code Online (Sandbox Code Playgroud) 我一直在关注这个英雄之旅教程.我有一个Django应用程序,其结构可以简化如下
apps/my_app/migrations/
apps/my_app/__init__.py
apps/my_app/urls.py
apps/my_app/views.py
frontend_stuff/js/ javascripts here
frontend_stuff/css/ css here
Run Code Online (Sandbox Code Playgroud)
英雄应用程序具有以下结构:
heroes/app/ contains all the .ts and .html files
heroes/node_modules/ angular2 and other libraries
heroes/styles.css css file
heroes/index.html
heroes/package.json and other config files
Run Code Online (Sandbox Code Playgroud)
目前我已经使用Django View构建了一个API,它通过URL处理Http请求并返回JSON.但是我应该在哪里/如何将这个Angular应用程序放入当前的Django应用程序中,以及如何在向Django后端发出初始GET请求时将Angular应用程序加载到浏览器中?
编辑:关于加载HTML和JS的更具体的问题:
由于Angular 2使用Component并且模板由Angular而不是Django呈现,如何让Angular"访问"那些html?@Luis提到这些文件应放在静态文件夹中,但是{% static %}Django语法对Angular没有意义.
同样,我想app/main(如下所示)也应放在静态文件夹中.但是如何引用Angular内部的位置?谢谢!
System.import('app/main')
.then(null, console.error.bind(console));
Run Code Online (Sandbox Code Playgroud)
更新:我按照这个答案使用/static/js/my_app/main,现在它"大部分"正常加载.到目前为止,我的所有js文件似乎都被加载了; 但控制台日志显示某些角度文件在加载成功之前会出现404错误.错误如下图所示:
system.src.js:1085 GET http://localhost:8000/static/js/node_modules/angular2/src/platform/browser.js 404 (NOT FOUND)fetchTextFromURL @ system.src.js:1085(anonymous function) @ system.src.js:1646ZoneAwarePromise @ angular2-polyfills.js:589(anonymous function) @ system.src.js:1645(anonymous function) @ system.src.js:2667(anonymous function) @ …Run Code Online (Sandbox Code Playgroud) angular ×4
c# ×4
asp.net-mvc ×2
django ×2
javascript ×2
wpf ×2
asp.net ×1
css ×1
html ×1
http-post ×1
python ×1
unit-testing ×1
xaml ×1