简体   繁体   English

在哪里以及如何在angularjs中使用nodejs

[英]where and how to use nodejs with angularjs

I am new to UI framework development. 我是UI框架开发的新手。 Currently my requirement is to work with anuglarjs and nodejs. 目前,我的要求是使用anuglarjs和nodejs。 I know there are few more like me who want to know the exact use.. 我知道还有像我这样的人想知道确切的用途..

I am confused as where i need to use nodejs in my application. 我感到困惑,因为我需要在应用程序中使用nodejs。 I tried to find a simple live demo example(plunker/jsfiddle) which used angularjs and nodejs but i couldn't find one. 我试图找到一个简单的实时演示示例(plunker / jsfiddle),该示例使用了angularjs和nodejs,但我找不到。

Currently i have written a small module using angularjs, which hits the java controller class and saves/get the data and display the data on the webpage. 目前,我已经使用angularjs编写了一个小模块,该模块可以访问java控制器类并保存/获取数据并在网页上显示数据。 Here i'm no where using nodejs. 在这里,我无处使用nodejs。 I tried to search in web to understand the use of nodejs with angularjs. 我尝试在网络中搜索以了解如何将nodejs与angularjs结合使用。 Any input's would be very helpful. 任何输入将非常有帮助。

Below is the sample javascript code i implemented using angularjs. 以下是我使用angularjs实现的示例JavaScript代码。

myDataCOntroller.js myDataCOntroller.js

//some code here

$scope.submitFormData = function(myForm){
        if(myForm.$valid)
         {

            MyDataService.saveOrGetData($scope.myReport).then(
                    function(response) {
                        $scope.myReport = response;
                    },
               function(errResponse){
                   console.error('Errorr');
                });

         }else{
            console.log("invalid form data!!");
         }
    }

myDataService.js myDataService.js

app.factory('myService',function($http,$q,$location){

    var MY_SERVICE_URI = $location.protocol()+'://'+$location.host()+':'+$location.port();

    var _repServiceFactory={};


    _repServiceFactory.saveOrGetData = function(myData){

         var deferred = $q.defer();
         var url = appURL+'/saveOrGetData.form';
         $http.post(url,JSON.stringify(myData))
            .then(
            function (response) {
                   deferred.resolve(response.data);
            },
            function(errResponse){
                console.error('Error while fetching data');
                deferred.reject(errResponse);
            }
        );
        return deferred.promise;
  }

PS: I know that Angularjs is client side, Nodejs is server side, both are using Javascript programming language. PS:我知道Angularjs是客户端,Nodejs是服务器端,两者都使用Javascript编程语言。 What i want to know is, what is the use of nodejs and where is it used in real time?? 我想知道的是,nodejs的用途是什么?实时使用在哪里?

If you are writing an AngularJS front end web application, you may never have to use NodeJS. 如果您正在编写AngularJS前端Web应用程序,则可能永远不必使用NodeJS。

If you need tooling (build scripts to compile Sass, linters, etc) in your development or deployment process you can use NodeJS task runners like Gulp , Grunt or Webpack . 如果在开发或部署过程中需要工具(用于编译Sass,lint等的构建脚本),则可以使用诸如GulpGruntWebpack之类的NodeJS任务运行程序

If you need to build a back end API, that stores and retrieves information, you can use Express or the whole MEAN stack . 如果您需要构建用于存储和检索信息的后端API,则可以使用Express或整个MEAN stack

* * * Also worth noting-- you mention hitting a Java class. * * *同样值得注意的是-您提到了打Java类。 If you are already using Java for the backend, then you probably won't be using any NodeJS for that purpose. 如果您已经在将Java用于后端,则可能不会为此目的使用任何NodeJS。 If you were just shortening JavaScript to "Java", please note they are separate and distinct languages. 如果您只是将JavaScript缩写为“ Java”,请注意它们是不同的语言。 * * * * * *

Web dev has 4 primary parts: Web开发人员包含4个主要部分:

For you to create an app that is realtime, you can use websockets, especially Socket.io ( which works really well with and is built for Node.js ) 为了创建实时应用程序,可以使用websockets,尤其是Socket.io它可以很好地与Node.js结合使用

WebSockets are essentially bi-directional ( low latency ) event-based tunnels where data can be passed from client-to-server , and server-to-client without the need to make an AJAX ( tcp ) request per message. WebSocket本质上是双向(基于低延迟 )基于事件的隧道,可以在其中从client-to-server以及从server-to-client传递数据,而无需对每条消息进行AJAX( tcp )请求。


This paired with a strong data-binding architecture ( API -> Angular Binding ) allows for customizable single page views, where the same data can be used to create a view, and update the back-end modal when the data gets updated. 这与强大的数据绑定体系结构( API -> Angular BindingAngular Binding使用,可实现可自定义的单页面视图,在该页面中,可以使用相同的数据创建视图,并在更新数据时更新后端模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM