【发布时间】:2017-05-24 15:48:23
【问题描述】:
我是 angular.js 的新手。我的 main.js 文件中有两个指令,每个指令在模板中都有一个输入文本字段。有一个 Html 页面 (index.html),我想将这些指令的文本字段与另一个输入文本字段一起使用,该输入文本字段是一个 Html 输入文本字段。
现在我希望用户在指令的文本字段中给出的任何输入都应该计算字符数,并且应该在第三个文本字段中打印总和,这是一个 Html 文本字段。
代码如下:-
Main.js 文件代码:
/// <reference path="angular.min.js" />
var app = angular.module("myApp", []);
app.directive("textbox1", function() {
return {
restrict: 'E',
scope: {
timezone : "@"
},
template: "<div> <input type='text' style='background-color:orange; height=21px; width:151px;' ng-model='txtval1' ng-change='updateval()' />{{txtval1.length}} </div>"
}
});
app.directive("textbox2", function() {
return {
restrict: 'E',
scope: {
timezone: "@"
},
template: "<div> <input type='text' style='background-color:orange; height=21px; width:151px;' ng-model='txtval2' ng-change='updateval()'/>{{txtval2.length}} </div>"
}
});
app.controller("myCtrl", function ($scope) {
$scope.updateval = function () {
console.log($scope.txtval1.value);
$scope.txtThird = ($scope.txtval1.length) + ($scope.txtval2.length);
}
});
HTML 页面代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<div ng-app="myApp">
<textbox1></textbox1>
<textbox2></textbox2>
<br/><br/>
<input type="text" ng-model="txtThird"/>
</div>
<script src="scripts/angular.min.js"></script>
<script src="scripts/main.js"></script>
</body>
</html>
【问题讨论】:
标签: angularjs