博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular语法:Controller As
阅读量:5321 次
发布时间:2019-06-14

本文共 2271 字,大约阅读时间需要 7 分钟。

这个东东我觉得很好哟。

数据可以在同一个页面的不同的controller之间自由穿梭。。。

当然,

https://thinkster.io/a-better-way-to-learn-angularjs/controllers

这个网址也不错哟。。。

https://thinkster.io/a-better-way-to-learn-angularjs

 

Controller As Syntax

While everything we've created in this example so far works fine, a possible issue we can come accross as our application grows is when we start nesting controllers. Since each controller gets assigned their own scope, controllers that are nested can have trouble accessing variables from the parent scope. Specifically when data is being read from a child controller, where the value is directly assigned to the parent $scope and not namespaced within an object (accessing $scope.data.message will work from a child controller but accessing $scope.message can break). The rule of thumb is to always have a dot when referencing variables from controllers in your angular expressions. We can enforce this by using the "controller as" syntax. This makes it so that your controllers can be directly referenced within the view. The "controller as" syntax is generally the preferred syntax for controllers.

Read  on the "controller as" syntax

Now let's update our code to use the "controller as". Since our scope becomes the this keyword in our controller, we'll need to create a reference to this so that we don't lose context of our controller when we create/call functions within our controller.

Read the  for the this keyword in javascript

Create a reference to this in our controller.

angular.module('app').controller('MainCtrl', function ($scope){  var self = this;

 

Remove $scope from our controller dependency, and use self instead of $scope.

angular.module('app').controller('MainCtrl', function (){  var self = this;  self.message = 'hello';  self.changeMessage = function(message){    self.message = message;  };});

 

Now, let's update our view to use the "controller as" syntax.

{

{ main.message }}

 

Now all of our variables in our Angular expressions contain a dot, and we're able to directly reference our controllers so that when we have nested or multiple nested controllers, we can access variables directly instead of using $parent.

转载于:https://www.cnblogs.com/aguncn/p/5778839.html

你可能感兴趣的文章
hbase 如何导入到mysql_hbase 的数据 怎么导出到 一个文件或者mysql里面
查看>>
html存入mysql数据库_html,jquery,ajax,servlet,mysql实现前端数据写入数据库
查看>>
perf mysql_MySQL性能分析工具(perf和Flame Graphs)
查看>>
分布式 mysql amoeba_用Amoeba构架MySQL分布式数据库环境
查看>>
mysql 数据库已运行时长_查看 MySQL 已经运行多长时间的方法
查看>>
shell 同步mysql数据库_利用shell脚本实现对mysql数据库的备份
查看>>
mysql mediumint 长度_mysql中的MEDIUMINT 最大可以是多少
查看>>
centos7怎么登录到mysql数据库_centos7安装 mysql数据库
查看>>
mysql数据为空怎么显示0_mySQL 统计数据时,数据为空的显示为0
查看>>
zimbra mysql stopping_ZIMBRA命令行方式常用的操作
查看>>
后台启动redis_Linux安装redis
查看>>
霍尼韦尔oh4502使用说明_霍尼韦尔变送器选型资料
查看>>
看拼音写词语生成器_期末复习:统编语文1~6年级上册看拼音写词语练习(可下载打印)...
查看>>
mysql least用法_MySQL LEAST()函数的问题不会返回结果
查看>>
mysql函数联结_MySQL ------ 高级联结 (自联结,自然联结,外联结,带聚合函数的联结)(十五)...
查看>>
python3怎么打开7z压缩文件_python使用7z解压apk包的方法
查看>>
python减法怎么办_Python数据偏移减法,上下两行减法,python,错位,相减
查看>>
python创建一个列表包含3-30能被3整除的数字_【python3小白上路系列】练习练习练习(四)...
查看>>
python语言pos_英语以外的语言的POS
查看>>
python里pass_Python 中pass关键字有哪些功能?
查看>>