GVKun编程网logo

angularjs – Passport.js,Express.js和Angular.js路由:它们如何共存?(angularjs与angular的区别)

14

在这里,我们将给大家分享关于angularjs–Passport.js,Express.js和Angular.js路由:它们如何共存?的知识,让您更了解angularjs与angular的区别的本质,

在这里,我们将给大家分享关于angularjs – Passport.js,Express.js和Angular.js路由:它们如何共存?的知识,让您更了解angularjs与angular的区别的本质,同时也会涉及到如何更有效地angularjs express nodejs的关系、angularjs – Angular JS和partials、angularjs – Angular Js路由谷歌Chrome问题、angularjs – Angular JS:动态载入CSS和JS文件的内容。

本文目录一览:

angularjs – Passport.js,Express.js和Angular.js路由:它们如何共存?(angularjs与angular的区别)

angularjs – Passport.js,Express.js和Angular.js路由:它们如何共存?(angularjs与angular的区别)

我道歉这个问题有点长,但我已经研究了一段时间了,真的需要解释所有的故事.

背景:应用程序基于MEAN堆栈,尝试使用Passport.js授权Facebook登录.

在Passport.js guide之后,我实现了类似于:

// HTML
    <a href="/connect/facebook" target="_self">Add a Facebook login</a>
// send to facebook to do the authentication
app.get('/connect/facebook',isLoggedIn,passport.authorize('facebook',{ scope : 'email' })
);
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',{
    successRedirect : '/profile',failureRedirect : '/profile'
    }));

注意html中的target = _self以跳过Angular路由.
显然,授权工作正常.但是,重定向不起作用,因为路由由Angular处理.授权后我永远不会登陆/配置文件(但在默认的Angular路线上).

因此,我尝试使用Passport.js here建议的自定义回调,希望将json数据传递给Angular,并让Angular进行路由.我最终做了类似的事情:

// In the controller
$http.get("/connect/facebook").success(function(data){
    // here I wait for json data from the server and do the routing
});
// I call this route from Angular
app.get('/connect/facebook',{ scope : 'email' })
);
// But Facebook lands here!
app.get('/connect/facebook/callback',function(req,res,next) {
    passport.authorize('facebook',function(err,user,info) {
        res.json({something:smtg});
        ...

Passport.js解释说,显然自定义回调适用于本地登录.但是你在这看到问题了吗?我从Angular打电话给/ connect / facebook,但我应该从/ connect / facebook / callback收到一些json.

我即将放弃Passport,但在此之前,您是否看到任何允许在FB授权后登陆/配置文件的解决方案,也许是自定义消息?非常感谢您的阅读.

编辑:
Passport-Facebook GitHub account报告了同样的问题.在那里发布了一些额外的尝试,但尚未解决.

解决方法

这比一个答案中描述的要深一些,但我会尝试开始指向正确的方向.

从本质上讲,Angular.js路由根本不是真正的HTML路由,而是恰好使用URL来使用最终用户的内部路由结构.请记住,Angular.js是一个客户端脚本,并且不需要整页重新加载,因为这将重新加载整个脚本.因此,/#用于欺骗浏览器跳转到已加载脚本中的特定代码位. (与HTML文档中的传统锚点位置相对).不幸的是(或幸运的是),HTML 5模式允许您隐藏网址的/#部分,因此您只需看http://somesite.com/#/omeroute,而不是看http://somesite.com/#/omeroute.但请放心,/#仍然存在. Angular.js使用HTML5 pushState(AKA HistoryAPI)来执行魔术替换.

鉴于此,如果您已调用服务器路由,则您在Angular.js脚本之外,并且任何再次加载角度脚本的调用将从头开始.实际上,如果没有完全重新加载,您无法从服务器调用Angular.js路由.因此,您实际上在这里进行双重路由重定向.您的服务器应该调用它的角度默认路由,将/#/ someroute附加到呼叫中. angular.js页面将加载,解析/#,并重定向到正确的角度路径.但请记住,如果对已加载的对象存在任何依赖性,则这些对象不再存在于内存中.因此,以这种方式访问​​的任何路由都应该像应用程序的入口点一样运行.

实际上,您应该尝试使用successRedirect:’#/ profile’,请记住,angular中的配置文件路径应该被视为app入口点.

希望这能让你开始.

angularjs express nodejs的关系

angularjs express nodejs的关系

  1. nodejs和express的关系

    express是nodejs的框架,使nodejs更易使用。和ruby和rails的关系一样

  2. angular 如何在express中使用

    angular 是一个javascript库,和express没啥关系。只要放在http服务器中即可使用。可以使用apache服务器,也可以使用express提供的http服务器,设置static文件夹,并放入static文件夹即可。

  3. 用express 4.x生成的项目如何运行?

    DEBUG=angularexpress ./bin/www node ./bin/www nodejs app.js

    app.js需要增加如下语句才能运行

  4. http.listen(3000, function(){
      console.log(''listening on *:3000'');
    });

    5. express的路由顺序(修改了)

app.use(express.static(path.join(__dirname, ''public'')));


//app.use(''/'', routes);

app.use(''/users'', users);


app.get(''/'', function (req, res) {

    res.sendfile(''index.html'');

});

修改这个路由 /  没有起作用

        优先使用static文件夹中的index.html,再往下查找路由。


6. angularjs不能生效的原因排查:

a. angularjs不能从google下载,下载下来放在本地可以解决此问题

b. angular.module(''todo'', [])和<html ng-app="todoApp ">不一致


angularjs – Angular JS和partials

angularjs – Angular JS和partials

是否可能嵌入html页面在另一个在angular js?

如果是这样,怎么办呢?

Here在他们的教程,部分不嵌入页面,但它像你不同的页面,当你点击其中一个项目。 (see demo)

是的,你可以使用ngInclude指令。

请参阅这里的文档和示例:http://docs.angularjs.org/api/ng.directive:ngInclude

angularjs – Angular Js路由谷歌Chrome问题

angularjs – Angular Js路由谷歌Chrome问题

使用下面提到的Code1在角度Js中进行基本路由并获得“ XMLHttpRequest无法加载交叉源请求仅支持协议方案:http,数据,chrome扩展,https,chrome-extension-resource”错误并且我们必须使用本地Web服务器解析相同的和下载的MAMP并保持我的html [login.html]在htdocs文件夹中启动服务器并用Code2中提到的[localhostURL / AngularJs / login.html’]替换templateUrl并获得错误错误:[ $sce:insecurl]下面给出了确切的错误,指导我在Google Chrome中修复相同的任何解决方案…

代码1

的index.html

<!DOCTYPE html>
 <html>
    <head>
    <title>Test</title>
    </head>
    <body ng-app="plunker">
    <div ng-view=""></div>    
    <script src="angular.min.js"></script>
    <script src="angular-route.js"></script>
    <script src="app.js"></script>  
   </body>
 </html>

app.js

var app = angular.module('plunker',['ngRoute']);
  app.config(function ($routeProvider) {
  $routeProvider.when('/login',{
        controller: '',templateUrl: 'login.html'
    }).otherwise({ 
        redirectTo: '/login' 
    });
 });

的login.html

Hello from login!

码2

所有其他事情都与app.js中的更改相同

var app = angular.module('plunker',['ngRoute']);
   app.config(function ($routeProvider) {
   $routeProvider.when('/login',templateUrl: 'http://localhost:8888/AngularJs/login.html'
    }).otherwise({ 
        redirectTo: '/login' 
    });
});

错误代码1: –
XMLHttpRequest无法加载file:// localhost / Users / karthicklove / Documents / Aptana Studio 3 Workspace / Object_Javascript / Angular_Js / Routing / login.html.交叉源请求仅支持协议方案:http,chrome-extension-resource.

错误代码2: – [$sce:insecurl] http://errors.angularjs.org/1.2.26/ $sce / insecurl?p0 = http:// localhost:8888 / AngularJs / login.html出错(本机)

我得到了类似的错误.我得到了一个解决方案,我们不能在AngularJS中使用Routing将它保存在你的文件系统中. (即)file:/// C:/Users/path/to/file.html?r = 4383065’如果您使用此URL运行,您将收到错误.路由提供程序将使用http协议.因此,您必须将代码放在localhost中,然后从浏览器localhost / foldername / index.html运行.不要直接从文件夹中运行它

并且还像这样更改模板URL

templateUrl: '/AngularJs/login.html'

如果你有疑问在这里发布.

angularjs – Angular JS:动态载入CSS和JS文件

angularjs – Angular JS:动态载入CSS和JS文件

我正在开发一个网络应用程序.我使用AngularJS将所有文件动态加载到UI中.
1.我有和index.html,所有文件将在点击或加载时动态加载.
//index.html
    <signin button> <signup button> // etc.,//on-clicking signin button a SignIn-page will load in the below div

    <div id="bodyview"></div>
    // end of index.html

现在我的登录页面看起来有点像下面的结构

/*a div where my accordion menu will load when the 
     *below submit button is clicked*/

    <div id="menu"></div>

    //other sign-in-form stuff will follow on...

    //submit button at the end
    <submit button> //only on clicking the submit button the menu will be loaded

现在我的问题是,虽然我可以加载手风琴menu.html文件,我无法加载它依赖的css和js文件.我已经研究了大多数stackoverflow讨论和很多其他..但没有一个为我工作.
任何人都可以帮助确定使用Angular JS加载css和js依赖关系的方法?

任何帮助不胜感激提前致谢

只需编写一个服务即可将CSS和JS文件添加到< head>

这是一个用于动态加载CSS文件的示例服务.您可以轻松地修改它来加载JS文件.

https://github.com/Yappli/angular-css-injector

今天关于angularjs – Passport.js,Express.js和Angular.js路由:它们如何共存?angularjs与angular的区别的介绍到此结束,谢谢您的阅读,有关angularjs express nodejs的关系、angularjs – Angular JS和partials、angularjs – Angular Js路由谷歌Chrome问题、angularjs – Angular JS:动态载入CSS和JS文件等更多相关知识的信息可以在本站进行查询。

本文标签: