2

在此示例中,代码有效

但是当我尝试在我的服务器上传递它时出现错误

ReferenceError: _ is not defined

这是js代码

// Declare app level module which depends on filters, and services
var app = angular.module('myApp', []);

function RegisterCtrl($scope, $location) 
{

  $scope.steps = [
    'Step 1: Team Info',
    'Step 2: Campaign Info',
    'Step 3: Campaign Media'
  ];
  $scope.selection = $scope.steps[0];

  $scope.getCurrentStepIndex = function()
  {
    // Get the index of the current step given selection
    return _.indexOf($scope.steps, $scope.selection);
  };

  // Go to a defined step index
  $scope.goToStep = function(index) 
  {
    if ( !_.isUndefined($scope.steps[index]) )
    {
      $scope.selection = $scope.steps[index];
    }
  };

  $scope.hasNextStep = function(){
    var stepIndex = $scope.getCurrentStepIndex();
    var nextStep = stepIndex + 1;
    // Return true if there is a next step, false if not
    return !_.isUndefined($scope.steps[nextStep]);
  };

  $scope.hasPreviousStep = function(){
    var stepIndex = $scope.getCurrentStepIndex();
    var previousStep = stepIndex - 1;
    // Return true if there is a next step, false if not
    return !_.isUndefined($scope.steps[previousStep]);
  };

  $scope.incrementStep = function() {
    if ( $scope.hasNextStep() )
    {
      var stepIndex = $scope.getCurrentStepIndex();
      var nextStep = stepIndex + 1;
      $scope.selection = $scope.steps[nextStep];
    }
  };

  $scope.decrementStep = function() {
    if ( $scope.hasPreviousStep() )
    {
      var stepIndex = $scope.getCurrentStepIndex();
      var previousStep = stepIndex - 1;
      $scope.selection = $scope.steps[previousStep];
    }
  };
}

的作用是什么

$scope.goToStep = function(index) 
  {
    if ( !_.isUndefined($scope.steps[index]) )
    {
      $scope.selection = $scope.steps[index];
    }
  };

为什么是错误???

4

1 回答 1

3

您需要将下划线库包含在 html 文件的 head 部分中,或者如果它是节点应用程序,则将其作为模块。

<script src="http://underscorejs.org/underscore-min.js"></script>

或者如果它的服务器端代码,请npm install underscore在您的项目根目录中执行,然后您可以执行

var _ = require('underscore');

于 2013-01-02T19:29:10.997 回答