下面的例子提供了三种事件:
<!DOCTYPE html>
<html ng-app="spicyApp1">
<head>
<script src="../public/js/angular.min.js"></script>
</head>
<body>
<div ng-app="spicyApp1" ng-controller="SpicyCtrl">
<button ng-click="chiliSpicy()">Chili</button>
<button ng-click="jalapenoSpicy($event)">$event</button>
<button ng-click="myParam('huangbiao')">myParam</button>
<p>The food is {{spice}}!</p>
</div>
</body>
</html>
<script>
var myApp = angular.module('spicyApp1', [])
myApp.controller('SpicyCtrl', [
'$scope',
function ($scope) {
$scope.spice = 'very'
// 自定义传递参数
$scope.myParam = function (paramStr) {
$scope.spice = paramStr
}
// 不带参数
$scope.chiliSpicy = function () {
$scope.spice = 'chili'
}
// 传递事件对象
$scope.jalapenoSpicy = function (eventObj) {
console.log(eventObj)
$scope.spice = 'jalapeño'
debugger
}
},
])
</script>
ng-controller 指令会创建新的子级 scope ,这样我们就会获得一个与DOM层级结构相对应的的基于继承关系的 scope 层级结构
<!DOCTYPE html>
<html ng-app="scopeInheritance">
<head>
<script src="../public/js/angular.min.js"></script>
<style>
div.spicy div {
padding: 10px;
border: solid 2px blue;
}
</style>
</head>
<body>
<div ng-app="scopeInheritance" class="spicy">
<div ng-controller="MainCtrl">
<p>Good {{timeOfDay}}, {{name}}!</p>
<div ng-controller="ChildCtrl">
<p>Good {{timeOfDay}}, {{name}} !</p>
<div ng-controller="GrandChildCtrl">
<p>Good {{timeOfDay}}, {{name}}!</p>
</div>
</div>
</div>
</div>
</body>
</html>
<script>
var myApp = angular.module('scopeInheritance', [])
myApp.controller('MainCtrl', [
'$scope',
function ($scope) {
$scope.timeOfDay = 'morning'
$scope.name = 'Nikki'
},
])
myApp.controller('ChildCtrl', [
'$scope',
function ($scope) {
$scope.name = 'Mattie'
},
])
myApp.controller('GrandChildCtrl', [
'$scope',
function ($scope) {
$scope.timeOfDay = 'evening'
$scope.name = 'Gingerbreak Baby'
},
])
</script>
本文地址:https://blog.csdn.net/hbiao68/article/details/107468664
如对本文有疑问, 点击进行留言回复!!
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
vert实践五——Json?Protocol Buffer?FlatBuffers?
[基于tensorflow的人脸检测] 基于神经网络的人脸检测8——验证训练好的神经网络
selenium + ajax抓取英雄联盟全部英雄的详细信息及多线程保存全部皮肤图片到本地
网友评论