简体   繁体   English

Angular js应用程序不起作用

[英]angular js app not working

this is my first time trying angular. 这是我第一次尝试角度。 I have created an app using angular but it doesn't seem to load. 我已使用angular创建了一个应用程序,但似乎未加载。 could someone take a look over my code and see if I am missing something simple here. 有人可以看看我的代码,看看我在这里是否缺少简单的东西。 Thanks. 谢谢。 Greg 格雷格

<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<meta charset=utf-8 />
<title>Angular JS Demo</title>
</head>
<body ng-controller="ctrl">

<script type="text/javascript">
var myApp = angular.module('myApp',[]);

function ctrl($scope){
$scope.rows = ['Paul','John','Lucie'];
$scope.temp = false;

$scope.addRow = function(){
$scope.temp = false;
$scope.addName="";
};

$scope.deleteRow = function(row){
$scope.rows.splice($scope.rows.indexOf(row),1);
};

$scope.plural = function (tab){
return tab.length > 1 ? 's': ''; 
};

$scope.addTemp = function(){
if($scope.temp) $scope.rows.pop(); 
else if($scope.addName) $scope.temp = true;

if($scope.addName) $scope.rows.push($scope.addName);
else $scope.temp = false;
};

$scope.isTemp = function(i){
return i==$scope.rows.length-1 && $scope.temp;
};

}

</script>

<h2>{{rows.length}} Friend{{plural(rows)}} <span ng-show="temp">?<small      class="muted"><em > (only {{rows.length-1}} actually....)</em></small></span>        </h2>
<form class="form-horizontal">
<span ng-class="{'input-append':addName}">
<input id="add" type="text" placeholder="Another one ?" ng-model="addName" ng-change="addTemp()"/>
<input type="submit" class="btn btn-primary" ng-click="addRow()" ng-show="addName" value="+ add"/>
</span>

<span class="search input-prepend" ng-class="{'input-append':search}">
  <span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="span2"  placeholder="Search" ng-model="search">
<button type="submit" class="btn btn-inverse" ng-click="search=''" ng-show="search" value="+ add"><i class="icon-remove icon-white"></i></button>
</span>
</form>
<table  class="table table-striped">
<tr ng-repeat="row in rows | filter : search"  ng-class="{'muted':isTemp($index)}">
  <td>{{$index+1}}</td>
  <td>{{row}}</td>
  <td>
  <button class="btn btn-danger btn-mini" ng-click="deleteRow(row)" ng-  hide="isTemp($index)"><i class="icon-trash icon-white"></i></button>
  </td>
  </tr>
  </table>

</body>
</html>

你忘了你的控制器申报您的应用程序在你的代码添加结束:

myApp.controller('ctrl', ctrl);

Here is the working copy of your code: 这是代码的工作副本:

<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<meta charset=utf-8 />
<title>Angular JS Demo</title>
</head>
<body ng-controller="ctrl">

<script type="text/javascript">
var myApp = angular.module('myApp',[]);

function ctrl($scope){
$scope.rows = ['Paul','John','Lucie'];
$scope.temp = false;

$scope.addRow = function(){
$scope.temp = false;
$scope.addName="";
};

$scope.deleteRow = function(row){
$scope.rows.splice($scope.rows.indexOf(row),1);
};

$scope.plural = function (tab){
return tab.length > 1 ? 's': ''; 
};

$scope.addTemp = function(){
if($scope.temp) $scope.rows.pop(); 
else if($scope.addName) $scope.temp = true;

if($scope.addName) $scope.rows.push($scope.addName);
else $scope.temp = false;
};

$scope.isTemp = function(i){
return i==$scope.rows.length-1 && $scope.temp;
};

}
myApp.controller('ctrl',ctrl);
</script>

<h2>{{rows.length}} Friend{{plural(rows)}} <span ng-show="temp">?<small      class="muted"><em > (only {{rows.length-1}} actually....)</em></small></span>        </h2>
<form class="form-horizontal">
<span ng-class="{'input-append':addName}">
<input id="add" type="text" placeholder="Another one ?" ng-model="addName" ng-change="addTemp()"/>
<input type="submit" class="btn btn-primary" ng-click="addRow()" ng-show="addName" value="+ add"/>
</span>

<span class="search input-prepend" ng-class="{'input-append':search}">
  <span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="span2"  placeholder="Search" ng-model="search">
<button type="submit" class="btn btn-inverse" ng-click="search=''" ng-show="search" value="+ add"><i class="icon-remove icon-white"></i></button>
</span>
</form>
<table  class="table table-striped">
<tr ng-repeat="row in rows | filter : search"  ng-class="{'muted':isTemp($index)}">
  <td>{{$index+1}}</td>
  <td>{{row}}</td>
  <td>
  <button class="btn btn-danger btn-mini" ng-click="deleteRow(row)" ng-  hide="isTemp($index)"><i class="icon-trash icon-white"></i></button>
  </td>
  </tr>
  </table>

</body>
</html>

the issue was you forgot to declare the controller in your app. 问题是您忘记在应用程序中声明控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM