【问题标题】:AngularJS, NodeJs and ExpressJSAngularJS、NodeJs 和 ExpressJS
【发布时间】:2015-07-17 00:04:06
【问题描述】:

这个程序只是为了学习如何使用angular、express和nodejs做一个总结。

我的图书馆是:

angular.js 角度路由.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);

【问题讨论】:

    标签: javascript html angularjs node.js


    【解决方案1】:

    您将urlparams 都以错误的方式传递给$http.get

    $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";
    });
    

    【讨论】:

    • 嗨 pankajparkar 非常感谢你。你说的对!!你对这个解释很有帮助!
    • @RodrigoNM 您能否将此答案标记为正确:)
    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2013-06-21
    • 2016-07-22
    • 2017-11-21
    相关资源
    最近更新 更多