angular js 实例参数学习

本文介绍了一个使用AngularJS实现的简单示例,通过自定义指令(myTest)和控制器(directive1)来展示双向数据绑定及DOM操作的基本用法。示例中定义了一个输入框用于接收城市名称,并通过指令显示该城市的天气。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]">
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script src="http://apps.bdimg.com/libs/angular.js/1.4.0-beta.4/angular.min.js"></script>
  <script>

  //directive文件directives.js中定义一个myTest  

//定义一个模块 angular.module('myApp',[]), 获取一个模块 angular.module('myApp')
var phonecatDirectives = angular.module('phonecatDirectives', []);    

//定义一个指令
phonecatDirectives.directive(
'myTest', function() { return { restrict: 'ACEM', require: '^ngModel', scope: { ngModel: '=' //变量 数据绑定 }, template: '<div><h4>Weather for {{ngModel}}</h4></div>' } }); //controller文件controller.js中定义directive1 定义控制器 var dtControllers = angular.module('dtControllers', []); dtControllers.controller('directive1',['$scope', function($scope) { $scope.name = 'this is tank test'; } ]); //在app文件app.js中整合controller,directive 模块 var phonecatApp = angular.module('phonecatApp', [ 'phonecatDirectives', 'dtControllers' ]); </script> </head> <body ng-app="phonecatApp"> <div ng-controller="directive1"> <input type="text" ng-model="city" placeholder="Enter a city" /> <my-test ng-model="city" ></my-test> <span my-test="exp" ng-model="city"></span> <!-- directive: my-test exp --> <span ng-model="city"></span> </div> </body> </html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值