栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Controller As方法访问继承的范围

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Controller As方法访问继承的范围

经过研究,我得出以下认识:

Controller-As方法不能代替使用

$scope
。两者都有其位置,可以/应该明智地一起使用。

  1. $scope
    确实执行该名称所隐含的含义:即,它在上定义了ViewModel属性
    $scope
    。这最适合与可使用
    $scope
    来驱动自己的逻辑或对其进行更改的嵌套控制器共享范围。
  2. Controler-As将整个控制器对象定义为具有命名范围的ViewModel(通过控制器的别名)。如果View决定是否要引用特定的控制器ViewModel,则此方法仅在View(而非其他控制器)中效果最佳。

这是一个例子:

var app = angular.module('myApp', []);// Then the controllers could choose whether they want to modify the inherited scope or not:app.controller("ParentCtrl", function($scope) {    this.prop1 = {      v: "prop1 from ParentCtrl"    };    $scope.prop1 = {      v: "defined on the scope by ParentCtrl"    };  })  .controller("Child1Ctrl", function($scope) {})  .controller("Child2Ctrl", function($scope) {    // here, I don't know about the "pc" alias    this.myProp = $scope.prop1.v + ", and changed by Child2Ctrl";  });<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script><body ng-app="myApp">  <div ng-controller="ParentCtrl as pc">     <div ng-controller="Child1Ctrl">        <div>I know about the "pc" alias: {{pc.prop1.v}}</div>     </div>     <div ng-controller="Child2Ctrl as ch2">       <div>I only care about my own ViewModel: {{ch2.myProp}}</div>    </div>  </div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号