繁体   English   中英

AngularJS,NodeJ和ExpressJS

[英]AngularJS, NodeJs and ExpressJS

该程序只是对学习如何工作angular,express和nodejs做一个总结。

我的图书馆是:

angular.js angular-route.js

我正在尝试运行此代码,但我遇到了该错误:

Uncaught TypeError: url.match is not a function (13:04:31:527 | error,  javascript)
  at completeRequest (public_html/js/libs/angular.js/angular.js:10437:27)
  at xhr.onreadystatechange              (public_html/js/libs/angular.js/angular.js:10404:11)

我的HTML是:

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="js/libs/angular.js/angular-resource.js" type="text/javascript"></script>
    <script src="js/libs/angular.js/angular-route.js" type="text/javascript"></script>
    <script src="script.js" type="text/javascript"></script>

</head>
<body ng-controller="AppCtrl" >

    <form >
        <input type="text" ng-model="url" size="80" aria-label="URL" />
        <br>
        <br>            Parameter 1: 
        <input type="text" ng-model="param1">
        <br>
        <br>
        Parameter 2: 
        <input type="text" ng-model="param2">
    </form>
    <br>
    <br>
     <button id="samplegetbtn" ng-click="click('http://localhost:8000/calc/param1/:param1/param2/:param2')">Sample GET</button>
     <br>
     <div >Sum: {{data}}</div>
    <br>
</body>

我的服务器端是:

var url = require('url');
//Require express, 
var express = require('express');
//and create an app
var app = express();

//app.get() which represents the HTTP GET method
app.get('/', function (req, res) {

  res.send('Hello World!');

});

//app.get() which represents the HTTP GET method
app.get('http://localhost:8000/calc/param1/:param1/param2/:param2', function (req, res) {

var a = parseInt(req.params.param1);
  console.log(a);
  var b = parseInt(req.params.param2);
  console.log(b);


var output = a + b;
  console.log(output);
  res.sendStatus(output);

});



var server = app.listen(8000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://' + host + ':' + port);

});

我的AngularJS脚本:

(function(angular) {
'use strict';
 angular.module('myCalc', ['ngRoute'])



 .controller('AppCtrl', function($scope, $http, $templateCache) {
    $scope.click = function(url) {
    $scope.url = url;
    var param1 = $scope.param1;
  var param2 = $scope.param2;

$http.get({ url: $scope.url, param1: $scope.param1, param2: $scope.param2, cache: $templateCache}).
 //    $http.get({ url: $scope.url,  cache: $templateCache}).
        success(function(data) {
        $scope.data = data;
       }).
        error(function(data) {
        $scope.data = data || "Request failed";
      });

  };

})


 .config(function($routeProvider, $locationProvider) {
   $routeProvider
   .when('http://localhost:8000/calc/param1/:param1/param2/:param2', {
    templateUrl: 'index.html',
    controller: 'AppCtrl'


   // configure html5 to get links working on jsfiddle
   $locationProvider.html5Mode(true);
 });
})(window.angular);

您为$http.get错误地传递了urlparams

$http.get(url, {configjson})

因为它的第一个参数是url,第二个参数是用于配置

$http.get($scope.url, {
    params: {
        param1: $scope.param1,
        param2: $scope.param2
    },
    cache: $templateCache
}).
success(function(data) {
    $scope.data = data;
}).
error(function(data) {
    $scope.data = data || "Request failed";
});

暂无
暂无

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

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