Angular $ watch问题 [英] Angular $watch problem

查看:93
本文介绍了Angular $ watch问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Angular。请告诉我为什么最底部的$ watch不起作用:



I am learning Angular. Please tell me why the $watch at the very bottom does not work:

<html>
<head>
	<link rel="Stylesheet" href="c.css" type="text/css" />
</head>
<body ng-app ng-controller='StudentListController'>
	<ul >
		<li ng-repeat='student in students'>
			<a href='/student/view/{{student.id}}' class="menu-{{student.valid}}">{{student.name}}</a>
			<input type="text" ng-model="student.quantity" />
		</li>
	</ul>
	<button ng-click="Sum()" class="menu-true">Sum</button>
	<div>{{sum}}</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
	var students = [{ name: 'Mary Contrary', id: '1', valid: 'true', quantity: 0 },
{ name: 'Jack Sprat', id: '2', valid: 'false', quantity: 0 },
{ name: 'Jill Hill', id: '3', valid: 'false', quantity: 0 }];

	function StudentListController($scope)
	{
		$scope.students = students;
		$scope.sum = 0;

		$scope.Sum = function ()
		{
			$scope.sum = 0;
			for (var i = 0;  i < $scope.students.length; i++)
			{
				$scope.sum = parseFloat($scope.sum) + parseFloat($scope.students[i].quantity);
			}
		};

		$scope.$watch($scope.students[1], $scope.Sum, 'true');
	}

</script>
</html>

推荐答案

最底层的手表不起作用:



watch at the very bottom does not work:

<html>
<head>
	<link rel="Stylesheet" href="c.css" type="text/css" />
</head>
<body ng-app ng-controller='StudentListController'>
	<ul >
		<li ng-repeat='student in students'>
			<a href='/student/view/{{student.id}}' class="menu-{{student.valid}}">{{student.name}}</a>
			<input type="text" ng-model="student.quantity" />
		</li>
	</ul>
	<button ng-click="Sum()" class="menu-true">Sum</button>
	<div>{{sum}}</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
	var students = [{ name: 'Mary Contrary', id: '1', valid: 'true', quantity: 0 },
{ name: 'Jack Sprat', id: '2', valid: 'false', quantity: 0 },
{ name: 'Jill Hill', id: '3', valid: 'false', quantity: 0 }];

	function StudentListController(


scope)
{
scope) {


scope.students = students;
scope.students = students;


这篇关于Angular $ watch问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆