【问题标题】:AngularJS controller not being called from routed templeturlAngularJS 控制器没有被路由的 Templeturl 调用
【发布时间】:2017-04-11 21:13:40
【问题描述】:

我是 angularjs 的新手。我正在尝试使用 index.html、app.js、searchtable.html 和 searchtable.js 制作应用程序。

index.html

<!DOCTYPE html>
<html lang="en" ng-app="myApp" class="no-js"> 
<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>DD AngularJS App</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css">
  <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css">
  <link rel="stylesheet" href="app.css">
  <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script>

</head>
<body ng-controller="HelloCtrl">
		
	<div class="container">
		<div class="page-header" style="margin: 0">			
			<h1>Hello World</h1>	
			</div>

			<ul class="menu" >						
				<li><a href="#searchtable">Search_Table</a></li>					
			</ul>	

		<div ng-view></div>
		
		<div class="panel panel-default">
			<div class="panel-heading">
				<h3 class="panel-title">Notes</h3>
			</div>
			<div class="panel-body">Click on any link to navigate</div>
			<div class="panel-body">DD First App: v<span app-version></span></div>
		</div>						
	</div>	
	  
  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="app.js"></script>
  <script src="views/helloworld/helloworld.js"></script>
  <script src="views/searchtable/searchtable.js"></script>
  <script src="views/sorttablecolumn/sorttablecolumn.js"></script>
  <script src="components/version/version.js"></script>
  <script src="components/version/version-directive.js"></script>
  <script src="components/version/interpolate-filter.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular-route.js"></script>
</body>
</html>
app.js

angular.module('myApp', [
  'ngRoute',
  'myApp.views.helloworld',
  'myApp.views.searchtable',  
  'myApp.version'
])

var helloApp = angular.module("myApp", ['ngRoute']);
helloApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider
    //.when('/',{ controller: 'HelloCtrl', templateUrl: '/index.html' })
    .otherwise({
      controller: 'HelloCtrl',
      templateUrl: 'views/helloworld/helloworld.html'
    })
    .when('/searchtable', {
      controller: 'searchtableCtrl',
      templateUrl: 'views/searchtable/searchtable.html'
    })

}]);
helloApp.controller("HelloCtrl", function($scope) {
  $scope.name = "Calvin Hobbes";
});

searchtable.html

<html lang="en" ng-app="myApp" class="no-js" ng-controller="searchtableCtrl">
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="app/views/searchtable/searchtable.js"></script>

<body>
  <div class="page-header" style="margin: 0">
    <h1>
      <!-- Heading goes here -->
      Add Table Row Dynamically
    </h1>
  </div>
  <div ng-view></div>
  <div style="padding-top: 15px">
    <!-- Summary goes here -->
    This demo shows how to add a table row dynamically.
  </div>
  <div style="padding: 10px 0px 20px 0px">
    <b>Instruction: </b>
    <!-- Instruction goes here -->
    Enter the company details and submit to add a row.
  </div>  
  <div style="padding:20px 0px 60px 0px">
    <span class="col-md-1" style="font-weight:bold">Search</span>
    <span class="col-md-3">
			<input type="text" class="form-control" ng-model="searchKeyword"/>
			</span>
  </div>

  <div style="padding-bottom:50px">
    <table style="width:100%">
      <tr>
        <td style="width:45%;padding-left:40px;vertical-align: top">
          <div style="padding-bottom:20px">
            <h3>Add a Company</h3>
          </div>
          <form class="form-horizontal" role="form" ng-submit="addRow()">
            <div class="form-group">
              <label class="col-md-2 control-label">Name</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="name" ng-model="name" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">Employees</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="employees" ng-model="employees" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">Headoffice</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="headoffice" ng-model="headoffice" />
              </div>
            </div>
            <div class="form-group">
              <div style="padding-left:110px">
                <input type="submit" value="Submit" class="btn btn-primary" />
              </div>
            </div>
          </form>
        </td>
        <td style="vertical-align: top">
          <table class="table">
            <tr>
              <th>Name</th>
              <th>Employees</th>
              <th>Head Office</th>
              <th>Action</th>
            </tr>
            <tr ng-repeat="company in companies  | filter: searchKeyword">
              <td>{{company.name}}
              </td>
              <td>{{company.employees}}
              </td>
              <td>{{company.headoffice}}
              </td>
              <td>
                <input type="button" value="Remove" class="btn btn-primary" ng-click="removeRow(company.name)" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </div>
</body>

</html>
searchtable.js

angular.module('myApp', ['ngRoute'])
console.log("search controller launched")
  //var helloApp = angular.module("myApp", ['ngRoute']);
  .config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/views/searchtable', {
      templateUrl: 'views/searchtable/searchtable.html',
      controller: 'searchtableCtrl'
    })
  }])
  //var searchApp = angular.module('myApp');
  .controller('searchtableCtrl', function($scope) {
    $scope.companies = [{
        'name': 'Infosys Technologies',
        'employees': 125000,
        'headoffice': 'Bangalore'
      },
      {
        'name': 'Cognizant Technologies',
        'employees': 100000,
        'headoffice': 'Bangalore'
      },
      {
        'name': 'Wipro',
        'employees': 115000,
        'headoffice': 'Bangalore'
      },
      {
        'name': 'Tata Consultancy Services (TCS)',
        'employees': 150000,
        'headoffice': 'Bangalore'
      },
      {
        'name': 'HCL Technologies',
        'employees': 90000,
        'headoffice': 'Noida'
      },
    ];
    $scope.addRow = function() {
      $scope.companies.push({
        'name': $scope.name,
        'employees': $scope.employees,
        'headoffice': $scope.headoffice
      });
      $scope.name = '';
      $scope.employees = '';
      $scope.headoffice = '';
    };

    $scope.removeRow = function(name) {
      var index = -1;
      var comArr = eval($scope.companies);
      for (var i = 0; i < comArr.length; i++) {
        if (comArr[i].name === name) {
          index = i;
          break;
        }
      }
      if (index === -1) {
        alert("Something gone wrong");
      }
      $scope.companies.splice(index, 1);
    };
  });

当我单击链接时,它会将我路由到 searchtable.html 页面,但控制器功能在 searchtable.js 中不起作用。如果我将该功能放在 app.js 中,它就可以工作。 谁能帮我解决这个问题

【问题讨论】:

  • 您有 2 个单独的 searchtableCtrl 实例正在运行....一个在路由中声明,一个在 ng-controllersearchtable.html 中应该只是一个模板部分,而不是一个完整页面
  • 您还加载了多个版本的 angular.js。真的好像你应该更彻底地学习一些角度教程
  • 你能解释一下吗,因为我是新手,正在学习 angularjs。先生,我没有得到您的第一条评论。您的第二条评论我理解并进行了更改。
  • 当您使用 $routeProvider.when 定义“/searchtable”时,您在此处指定了 searchtableCtrl。但是您还通过 ng-controller='searchtableCtrl' 在模板中指定了它。您正在使状态加载控制器,然后加载它的另一个实例。可以去掉 ng-controller='searchtableCtrl'
  • 我已经从模板中删除了它,但仍然无法工作@elpddev

标签: javascript html angularjs node.js


【解决方案1】:

你加载一些文件两次,searchtable.js 应该看起来更像:

<div class="page-header" style="margin: 0" ng-controller="searchtableCtrl">
    <h1>
      <!-- Heading goes here -->
      Add Table Row Dynamically
    </h1>
  </div>
  <div ng-view></div>
  <div style="padding-top: 15px">
    <!-- Summary goes here -->
    This demo shows how to add a table row dynamically.
  </div>
  <div style="padding: 10px 0px 20px 0px">
    <b>Instruction: </b>
    <!-- Instruction goes here -->
    Enter the company details and submit to add a row.
  </div>  
  <div style="padding:20px 0px 60px 0px">
    <span class="col-md-1" style="font-weight:bold">Search</span>
    <span class="col-md-3">
            <input type="text" class="form-control" ng-model="searchKeyword"/>
            </span>
  </div>

  <div style="padding-bottom:50px">
    <table style="width:100%">
      <tr>
        <td style="width:45%;padding-left:40px;vertical-align: top">
          <div style="padding-bottom:20px">
            <h3>Add a Company</h3>
          </div>
          <form class="form-horizontal" role="form" ng-submit="addRow()">
            <div class="form-group">
              <label class="col-md-2 control-label">Name</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="name" ng-model="name" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">Employees</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="employees" ng-model="employees" />
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label">Headoffice</label>
              <div class="col-md-4">
                <input type="text" class="form-control" name="headoffice" ng-model="headoffice" />
              </div>
            </div>
            <div class="form-group">
              <div style="padding-left:110px">
                <input type="submit" value="Submit" class="btn btn-primary" />
              </div>
            </div>
          </form>
        </td>
        <td style="vertical-align: top">
          <table class="table">
            <tr>
              <th>Name</th>
              <th>Employees</th>
              <th>Head Office</th>
              <th>Action</th>
            </tr>
            <tr ng-repeat="company in companies  | filter: searchKeyword">
              <td>{{company.name}}
              </td>
              <td>{{company.employees}}
              </td>
              <td>{{company.headoffice}}
              </td>
              <td>
                <input type="button" value="Remove" class="btn btn-primary" ng-click="removeRow(company.name)" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </div>

首先,您需要真正阅读文档 (https://angularjs.org/)。您的代码中有太多架构错误

例如: - 你定义你的应用程序三遍!!! (在 app.js 和 searchtable.js 中) - 您在 searchtable.js 中有一个 concole.log 位于错误的位置 - 你有多个完整的 html 文件,没有部分文件.....

【讨论】:

  • 我删除了重复的加载文件,但仍然无法正常工作@DMCISSOKHO
  • 您还可以在控制器中重新定义角度:angular.module('myApp', ['ngRoute'])。
猜你喜欢
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
相关资源
最近更新 更多