Angularjs 使用yeoman发电机后未加载

Angularjs 使用yeoman发电机后未加载,angularjs,yeoman,Angularjs,Yeoman,我已经使用yeoman generator生成了我的应用程序,我决定将我的应用程序复制到一个干净的文件夹中,但是由于rounting没有按预期工作,模板根本没有加载 我的应用程序js 'use strict'; angular .module('angularJsApp', ['ngRoute']) .config(function ($routeProvider) { $routeProvider .when('/',

我已经使用yeoman generator生成了我的应用程序,我决定将我的应用程序复制到一个干净的文件夹中,但是由于rounting没有按预期工作,模板根本没有加载

我的应用程序js

  'use strict';

    angular
      .module('angularJsApp', ['ngRoute'])
      .config(function ($routeProvider) {
        $routeProvider
          .when('/', {
            templateUrl: 'views/home.html',
            controller: 'main',
            controllerAs: 'mainController'
          })
          .when('/users', {
            templateUrl: 'views/users.html',
            controller: 'main',
            controllerAs: 'mainController'
          })
          .when('/active_users', {
            templateUrl: 'views/active_users.html',
            controller: 'second',
            controllerAs: 'secondController'
          })
          .otherwise({
            redirectTo: '/'
          });
      });
控制器

'use strict';


// users factory
angular.module('angularJsApp').factory("myFactory", function() {

    var existingEntries = JSON.parse(localStorage.getItem("allEntries"));

    var persoane = existingEntries;

    var factory = {};

    factory.getPersons = function() {
        return {
            existingEntries: existingEntries,
            persoane: persoane
        };
    }

    return factory;

});




// modules
angular.module('angularJsApp').controller('main', function($scope, myFactory) {

    var dataMain = this;


    $scope.adaugaperson = function() {

        var name = $scope.newName;
        var lastname = $scope.newLastName;
        var tel = $scope.newPhone;
        var email = $scope.newEmail;
        var age = $scope.newAge;
        var gender = $scope.newGender;



        var entry = {
            "name": name,
            "lastname": lastname,
            "tel": tel,
            "email": email,
            "age": age,
            "gender": gender,
            "isChecked": false
        };


        // sf not having any entry, crate 
        if ($scope.existingEntries == null) $scope.existingEntries = [];
        localStorage.setItem("entry", JSON.stringify(entry));
        // save allEntries back to local storage
        $scope.existingEntries.push(entry);

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));

        // reset the form 
        $scope.newName = '';
        $scope.newLastName = '';
        $scope.newPhone = '';
        $scope.newEmail = '';
        $scope.newAge = '';
        $scope.newGender = '';

    };

    // get persons
    initPersons();

    function initPersons() {

        $scope.persoane = myFactory.getPersons().persoane;
        $scope.existingEntries = myFactory.getPersons().existingEntries;
    }


    // remove person
    $scope.remove = function(indexNumber) {

        $scope.existingEntries.splice(indexNumber, 1);
        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));

    }


    // var for index targeting
    var indexValue = undefined;


    // populate form 
    $scope.populate = function(indexNumber) {
        indexValue = indexNumber;
        $scope.newName = $scope.existingEntries[indexNumber]["name"]
        $scope.newLastName = $scope.existingEntries[indexNumber]["lastname"];
        $scope.newPhone = $scope.existingEntries[indexNumber]["tel"];
        $scope.newEmail = $scope.existingEntries[indexNumber]["email"];
        $scope.newAge = $scope.existingEntries[indexNumber]["age"];
        $scope.newGender = $scope.existingEntries[indexNumber]["gender"];
    }


    // save edited user
    $scope.editUser = function(indexNumber) {
        indexNumber = indexValue;
        $scope.existingEntries[indexValue]["name"] = $scope.newName;
        $scope.existingEntries[indexValue]["lastname"] = $scope.newLastName;;
        $scope.existingEntries[indexValue]["tel"] = $scope.newPhone;
        $scope.existingEntries[indexValue]["email"] = $scope.newEmail;
        $scope.existingEntries[indexValue]["age"] = $scope.newAge
        $scope.existingEntries[indexValue]["gender"] = $scope.newGender;

        $scope.newName = "";
        $scope.newLastName = "";
        $scope.newPhone = "";
        $scope.newEmail = "";
        $scope.newAge = "";
        $scope.newGender = "";

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));


    }


    // active users state
    $scope.activeUsers = function(indexNumber) {
        indexValue = indexNumber;
        $scope.boolVal = $scope.existingEntries[indexNumber]["isChecked"];

        if ($scope.boolVal === false) {
            $scope.existingEntries[indexNumber]["isChecked"] = true;
        } else {
            $scope.existingEntries[indexNumber]["isChecked"] = false;
        }

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));
    }
});



// controler for users active
angular.module('angularJsApp').controller('second', function($scope, myFactory) {

    init();

    function init() {
        $scope.persoane = myFactory.getPersons().persoane;
        $scope.existingEntries = myFactory.getPersons().existingEntries;
    }
});
HTML

<!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
  <head>
    <meta charset="utf-8">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
    <!-- build:css(.) styles/vendor.css -->
    <!-- bower:css -->
    <link rel="stylesheet" href="components/bootstrap/dist/css/bootstrap.css" />
    <!-- endbower -->
    <!-- endbuild -->
    <!-- build:css(.tmp) styles/main.css -->
    <link rel="stylesheet" href="styles/main.css">
    <!-- endbuild -->
  </head>
  <body ng-app="angularJsApp">
    <!--[if lte IE 8]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div class="header">
      <div class="navbar navbar-default" role="navigation">
        <div class="container">
          <div class="navbar-header">

            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#js-navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            <a class="navbar-brand" href="#/">angularJs</a>
          </div>

          <div class="collapse navbar-collapse" id="js-navbar-collapse">

            <ul class="nav navbar-nav">
              <li><a href="#/">Home</a></li>
              <li><a ng-href="#!/active_users">Active Users</a></li>
              <li><a ng-href="#!/users/">Users</a></li>
            </ul>
          </div>
        </div>
      </div>
    </div>

    <div class="container">
    <div ng-view>

    </div>


    </div>

    <div class="footer">
      <div class="container">
      </div>
    </div>

    <!-- build:js(.) scripts/vendor.js -->
    <!-- bower:js -->
    <script src="components/jquery/dist/jquery.js"></script>
    <script src="components/angular/angular.js"></script>
    <script src="components/bootstrap/dist/js/bootstrap.js"></script>

    <!-- endbower -->
    <!-- endbuild -->

        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/main.js"></script>
        <!-- endbuild -->
</body>
</html>

切换导航

从没有指定模块的
html
标记中删除
ng-app
指令,显然它不允许将
ng-app=“angularJsApp”
放在
主体
标记上

来自文档

'use strict';


// users factory
angular.module('angularJsApp').factory("myFactory", function() {

    var existingEntries = JSON.parse(localStorage.getItem("allEntries"));

    var persoane = existingEntries;

    var factory = {};

    factory.getPersons = function() {
        return {
            existingEntries: existingEntries,
            persoane: persoane
        };
    }

    return factory;

});




// modules
angular.module('angularJsApp').controller('main', function($scope, myFactory) {

    var dataMain = this;


    $scope.adaugaperson = function() {

        var name = $scope.newName;
        var lastname = $scope.newLastName;
        var tel = $scope.newPhone;
        var email = $scope.newEmail;
        var age = $scope.newAge;
        var gender = $scope.newGender;



        var entry = {
            "name": name,
            "lastname": lastname,
            "tel": tel,
            "email": email,
            "age": age,
            "gender": gender,
            "isChecked": false
        };


        // sf not having any entry, crate 
        if ($scope.existingEntries == null) $scope.existingEntries = [];
        localStorage.setItem("entry", JSON.stringify(entry));
        // save allEntries back to local storage
        $scope.existingEntries.push(entry);

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));

        // reset the form 
        $scope.newName = '';
        $scope.newLastName = '';
        $scope.newPhone = '';
        $scope.newEmail = '';
        $scope.newAge = '';
        $scope.newGender = '';

    };

    // get persons
    initPersons();

    function initPersons() {

        $scope.persoane = myFactory.getPersons().persoane;
        $scope.existingEntries = myFactory.getPersons().existingEntries;
    }


    // remove person
    $scope.remove = function(indexNumber) {

        $scope.existingEntries.splice(indexNumber, 1);
        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));

    }


    // var for index targeting
    var indexValue = undefined;


    // populate form 
    $scope.populate = function(indexNumber) {
        indexValue = indexNumber;
        $scope.newName = $scope.existingEntries[indexNumber]["name"]
        $scope.newLastName = $scope.existingEntries[indexNumber]["lastname"];
        $scope.newPhone = $scope.existingEntries[indexNumber]["tel"];
        $scope.newEmail = $scope.existingEntries[indexNumber]["email"];
        $scope.newAge = $scope.existingEntries[indexNumber]["age"];
        $scope.newGender = $scope.existingEntries[indexNumber]["gender"];
    }


    // save edited user
    $scope.editUser = function(indexNumber) {
        indexNumber = indexValue;
        $scope.existingEntries[indexValue]["name"] = $scope.newName;
        $scope.existingEntries[indexValue]["lastname"] = $scope.newLastName;;
        $scope.existingEntries[indexValue]["tel"] = $scope.newPhone;
        $scope.existingEntries[indexValue]["email"] = $scope.newEmail;
        $scope.existingEntries[indexValue]["age"] = $scope.newAge
        $scope.existingEntries[indexValue]["gender"] = $scope.newGender;

        $scope.newName = "";
        $scope.newLastName = "";
        $scope.newPhone = "";
        $scope.newEmail = "";
        $scope.newAge = "";
        $scope.newGender = "";

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));


    }


    // active users state
    $scope.activeUsers = function(indexNumber) {
        indexValue = indexNumber;
        $scope.boolVal = $scope.existingEntries[indexNumber]["isChecked"];

        if ($scope.boolVal === false) {
            $scope.existingEntries[indexNumber]["isChecked"] = true;
        } else {
            $scope.existingEntries[indexNumber]["isChecked"] = false;
        }

        localStorage.setItem("allEntries", JSON.stringify($scope.existingEntries));
    }
});



// controler for users active
angular.module('angularJsApp').controller('second', function($scope, myFactory) {

    init();

    function init() {
        $scope.persoane = myFactory.getPersons().persoane;
        $scope.existingEntries = myFactory.getPersons().existingEntries;
    }
});
每个HTML只能自动引导一个AngularJS应用程序 文件文档中找到的第一个ngApp将用于定义 作为应用程序自动引导的根元素

您还必须在
angular.js
之后添加
angular-route.js

<script src="components/angular/angular.js"></script>
<script src="components/angular/angular-route.js"></script>


I'v完成此操作,现在我在控制台中发现一个错误:错误:[$injector:modulerr]未能实例化模块angularJsApp,原因是:[$injector:modulerr]未能实例化模块ngRoute,原因是:[$injector:nomod]模块“ngRoute”不可用!凉的现在工作!!谢谢!!但我还有一个问题。。。我的应用程序用于从表单中获取一些数据,并将这些数据存储到localstorage,然后将用户打印到表中。。。每次添加新用户时,我想表应该更新,但我需要刷新页面以查看新添加的用户。我已经将grunt用于本地主机服务器,现在我使用的是live server