角度JS不起作用
Angular JS not working
我学习,这是我第一天的角度js! 虽然我已经了解了模型控制器视图在angular js
中的工作原理,但 fowllowing 代码不显示变量,而是在没有ng-repeat
工作的情况下给出正常的 {{}} HTML
视图:
<html ng-app='myApp'>
<head>
<title>Your Shopping Cart</title>
</head>
<body ng-controller='CartController'>
<h1>Your Order</h1>
<div ng-repeat='item in items'>
<span>{{item.title}}</span>
<input ng-model='item.quantity'>
<span>{{item.price | currency}}</span>
<span>{{item.price * item.quantity | currency}}</span>
<button ng-click="remove($index)">Remove</button>
</div>
<script src="lib/angular.js"></script>
<script>
function CartController($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}
</script>
</body>
</html>
代码有什么问题?
只需替换
<html ng-app='myApp'>
跟
<html ng-app>
它应该有效。
使用 ng-app='myApp',你可以告诉 angularjs 你有一个名为 myApp 的模块。但是您没有定义任何模块。
你应该定义你的myApp
模块:
var app = angular.module('myApp', []);
app.controller('CartController', ['$scope', function($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}]);
演示
基本上你没有在控制器中定义module
。
<script>
angular.module('myApp', []); // add this line
function CartController($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}
</script>
您在普伦克的演示
相关文章:
- Js.erb VS按钮标记-不'不起作用.为什么?
- 为什么indexOf在这个js代码中不起作用
- 淘汰赛.JS'启用'长度绑定条件不起作用
- JS动态添加字段-删除按钮不起作用
- Knockout.JS标签在foreach内部不起作用
- 角度 JS + 重定向不起作用
- JS中的乘法不起作用
- Angular js+ng repeat+字母数字索引不起作用
- 角度Js ng-disabled不起作用
- 路径 lo 加载本地主机子文件夹中的文件不起作用.js仅在基路径中
- Jquery Carousel 不起作用(JS 问题)
- 验证方法在主干网中不起作用.js
- 应用程序.js在骨干中不起作用.js + 要求.js
- 路由在角度下不起作用.js - 没有控制台或语法错误
- Int from for 循环在函数中不起作用 - JS
- 为什么代码不起作用JS
- 为什么这个代码在iPad Pro上不起作用?(JS)
- .将Integer推入数组不起作用JS/JQuery
- 我的下拉列表不起作用-JS有问题
- Bootstrap弹出,粘贴不起作用.JS加载问题