Angular+Node產生隨機數的方法,angularnode

來源:互聯網
上載者:User

Angular+Node產生隨機數的方法,angularnode

本文執行個體講述了Angular+Node產生隨機數的方法。分享給大家供大家參考,具體如下:

以前寫過一個PHP產生隨機數,然後jquey ajax擷取,再jQuery改變文本的隨機數的程式

現在用Angular 和 Node來重寫一下

Angular的好處是雙向繫結,這樣直接設定變數,不用再重新設定了

Node的好處我目前還不是很理解,可以通過count這個例子來說明一下Node和PHP的不同之處

當然了,最大的好處就是,前後端都可以用JavaScript來寫了,這樣的話Javascript的水平就會大幅提高!

Node

app.js

var express = require('express');var app = express();var count = 0;app.get('/', function (req, res) { res.header('Access-Control-Allow-Origin', '*'); var x = Math.floor(Math.random() * 1e6); res.send(String(x)); console.log(count++);});app.listen(3000, function () { console.log('Example app listening on port 3000!');});res.header('Access-Control-Allow-Origin', '*');

這個涉及到跨域的問題,加上這句話就不是本地的也能訪問了,以後要做hybrid APP這時必須要用的
然後就是產生一個隨機數了,關鍵是count這個變數,後面執行的時候每訪問一次,它都不一樣,說明Node是常駐記憶體的,不想PHP,載入完了事

Angular

<!DOCTYPE html><html><head> <meta charset="utf-8"> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body> <div ng-app="myApp" ng-controller="customersCtrl">  {{names}}  <button ng-click="myClick()">擷取簽到碼</button> </div> <script>  var app = angular.module('myApp', []);app.controller('customersCtrl', function($scope, $http) { $scope.myClick = function(){  $http.get("http://localhost:3000").success(function (response) {$scope.names = response;}); }}); </script></body></html>

有一些概念:

module

ng-app 模組,目前的理解是模組化

Controller

ng-controller 控制器 是個 對象
對象包含成員,用$scope訪問

PS:這裡再為大家提供兩款功能類似的線上工具供大家參考:

線上隨機數字/字串產生工具:
http://tools.jb51.net/aideddesign/suijishu

高強度密碼產生器:
http://tools.jb51.net/password/CreateStrongPassword

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.