【发布时间】:2023-03-28 03:12:01
【问题描述】:
我对 AngularJS 很陌生,我发现这个例子与 ng-model 和 ng-bind 指令有关:
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="page" tagdir="/WEB-INF/tags" %>
<page:angular-template>
<jsp:attribute name="title">
AngularJS ng-bind ng-model
</jsp:attribute>
<jsp:body>
<div ng-app>
<h1>AngularJS ng-bind ng-model</h1>
<div>
<table class="table table-striped">
<tr>
<td><b>Username:</b></td> <td><input type="text" ng-model="username"/></td>
</tr>
<tr>
<td><b>Password:</b></td> <td><input type="text" ng-model="passwords"/></td>
</tr>
<tr>
<td><b>authorization:</b></td> <td><input type="text" ng-model="authorization"/> </td>
</tr>
</table>
</div>
Hi {{ username }} welcome to Java Spring MVC Integrated with AngularJS by Google and HTML5<br/>
You password is {{ passwords }} <br/>
and login as <span ng-bind="authorization"></span> <br/>
</div>
</jsp:body>
</page:angular-template>
所以在我看来,基本上它只显示一个表单,并且每个 input 元素都通过 ng-model 与 model 属性 相关联strong> 属性。
然后,在同一页面中,我可以通过诸如 {{ username }} 之类的 AngularJS 表达式或使用 ng-bind 来检索这些模型属性的值> 指令。比如:
<span ng-bind="authorization">
这意味着在这个范围内,它应该显示插入到具有 ng-model="authorization"
的输入标签中的值我的推理正确吗?
我的疑问是:在使用 AngularJS 时,我可以避免通过控制器吗?我已经看到如何将这些值放入 $scope 服务的属性中。
例如,输入标签 ng-model 是 $scope 服务的特定属性(以这种方式将视图关联到控制器)。
我可以避免通过控制器吗?
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-scope angular-ngmodel