当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJs学习第五篇从Controller控制器谈谈$scope作用域

AngularJs学习第五篇从Controller控制器谈谈$scope作用域

2019年05月28日  | 移动技术网IT编程  | 我要评论
controller的创建 angularjs controller使用无处不在,在里代码演示比较简单的创建工作。 <!doctype html>

controller的创建

angularjs controller使用无处不在,在里代码演示比较简单的创建工作。

<!doctype html>
<html xmlns="http://www.w.org//xhtml" ng-app="exampleapp">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-"/>
<title>app</title>
<script src="angular.js"></script>
<link href="bootstrap-theme.css" rel="stylesheet" />
<link href="bootstrap.css" rel="stylesheet" />
<script>
angular.module("exampleapp", [])
.controller("defaultctrl", function ($scope) {
$scope.setinput = function (value) {
console.log("print:" + value);
}
});
</script>
</head>
<body ng-controller="defaultctrl"> 
<div class="well">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="setinput(value)">click</button>
</div>
</div>
</body>
</html> 

  在这控制很简单,首先我在html 中添加了 ng-app 属性,表示module的作用范围。

在 body 中添加了 ng-controller 表示 defaultctrl 控制器的作用范围。

input 便签中ng-model 指令的是绑定数据,双向数据绑定(mvvm)。

$scope 是angularjs内置的作用域。

此实例的只是把输入的值打印到控制台中,如图:

仅此而已,简单吧。

多个控制器controller作用域问题

现在我们来改造下程序,

<body >
<div class="well" ng-controller="defaultctrl">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="setinput(value)">click</button>
</div>
</div>
<div class="well" ng-controller="defaultctrl">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="setinput(value)">click</button>
</div>
</div>
</body> 

  其余代码不变,我只是把放到body 中的属性 ng-controller 放到了两个div中。我重用了defaultctrl控制器,猜想下,如果我在第一个input标签输入内容,我点击第二个控制器的button按钮,会出现你所期望的结果吗?

结果是否和你想你的一样呢,大家可以看到这个结果为undefined. 在个很好解释,应为他们的作用域不同,虽然你重复使用了统一控制器,但是在创建作用域确实不同的。

调用的工厂函数会返回不同的作用域。

那么如何进行不同作用域之间的访问呢,在angularjs中对于作用域访问有个$rootscope 。

在这里有三个函数需要介绍下,

$on(name,handler) 注册一个事件处理函数,该函数在特定的事件被当前作用域收到时将被调用。

$emit(name,args) 向当前父作用域发送一个事件,直至根作用域。

$broadcast(name,args) 向当前作用域下的子作用域发送一个事件,参数是事件名称以及一个用于作用向事件提供额外数据的对象。

现在来更改下代码:

<script>
angular.module("exampleapp", [])
.controller("defaultctrl", function ($scope,$rootscope) {
$scope.$on("updatevalue", function (event, args) {
$scope.input = args.zip;
});
$scope.setinput = function (value) {
$rootscope.$broadcast("updatevalue", { zip: value });
console.log("print:" + $scope.input);
}
$scope.copy = function () {
console.log("copy:" + $scope.input);
};
});
</script> 
<div class="well" ng-controller="defaultctrl">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="copy()">copy</button>
</div>
</div> 

  在段代码中我添加了几个函数,同时改变了第二个控制器的函数。

结果:

确实发生了。

controller继承

<script>
angular.module("exampleapp", [])
.controller("defaultctrl", function ($scope, $rootscope) {
//$scope.$on("updatevalue", function (event, args) {
// $scope.input = args.zip;
//});
$scope.setinput = function (value) {
//$rootscope.$broadcast("updatevalue", { zip: value });
$scope.input = value;
console.log("print:" + $scope.input);
}
$scope.copy = function () {
console.log("copy:" + $scope.input);
};
})
.controller("simplectrl", function ($scope) {
$scope.copy = function () {
console.log("copy:" + $scope.input);
};
});
</script>
<body ng-controller="defaultctrl">
<div class="well">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="setinput(value)">click</button>
</div>
</div>
<div class="well" ng-controller="simplectrl">
<h>count</h>
<div class="form-group">
<input class="form-control" required ng-model="value" />
<button ng-click="copy()">copy</button>
</div>
</div>
</body> 

  我添加了一个控制器,simplectrl 仔细观察下,发现defaultctrl 包含了simplectrl 中,所以作用simple 也继承 了。

结果图:发下我在第一个窗中输入时,第二个也变了,应为都是同一个value。

$scope 作用域问题,在使用controller时 要明白其作用域。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网