我是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>
&#13;
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";
});
&#13;
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>
&#13;
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);
};
});
&#13;
当我点击链接时,我将其路由到searchtable.html页面,但控制器功能无法从searchtable.js运行。如果我把这个功能放在app.js中就行了。 请有人帮我这件事
答案 0 :(得分:0)
你加载一些文件两次,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文件,没有部分.......