我創建了一個自定義指令,它顯示了一個列表和選擇框。爲什麼Angular自定義指令作用域會影響父控制器作用域?
請按照下面的代碼,
<!DOCTYPE html>
<html lang="en">
<head>
<title>Template</title>
<script type="text/javascript" src="/home/rahul/Installers/jquery-3.0.0.js"></script>
<link rel="stylesheet" type="text/css" href="/home/rahul/Installers/Bootstrapv3.0.2/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/home/rahul/Installers/Bootstrapv3.0.2/css/bootstrap-theme.css">
<script type="text/javascript" src="/home/rahul/Installers/Bootstrapv3.0.2/js/bootstrap.js"></script>
<script type="text/javascript" src="/home/rahul/Installers/angular.js"></script>
<script type="text/javascript">
angular.module("app",[]);
angular.module("app").controller("myctrl",myctrl);
angular.module("app").controller("childCtrl",childCtrl);
angular.module("app").directive("loadFilms",loadFilms);
myctrl.$inject = ["$scope"];
childCtrl.$inject = ["$scope"];
function myctrl($scope){
var vm = this;
vm.title = "Directive and nested scoping";
vm.actorName = "Amithabh Bachhan";
$scope.year = ["70's","80's","90's"];
}
function childCtrl($scope){
var vm = this;
vm.selectedYear = "";
vm.moviesObj = {
"70's" : ["Anand","Lawaris","Kala Pathhar","Deewar","Amar Akbar Anthony","Mili"],
"80's" : ["Sharabi","Kaalia","Silsila","Satte Pe Satta","Nastik","Shahensha"],
"90's" : ["Hum","Aaj Ka Arjun", "Ajooba","Khuda Gawah","Ganga Jamuna Saraswati","Lal Badshah"]
}
vm.movies = [];
vm.getMovies = function(){
vm.movies = vm.moviesObj[vm.selectedYear];
}
}
function loadFilms(){
return {
restrict : "EA",
controller : "childCtrl",
controllerAs : "vm",
scope : true,
template : function(tElem, tAttrs){
var str = "<select ng-model='vm.selectedYear' data-ng-options='y as y for y in year' " + "class='form-control' ng-change='vm.getMovies()'>" +
"<option value=''>Select</option>" +
"</select>" +
"<br />" +
"<ol class='slide-animate-container'>" +
" <li class='slide-animate' ng-repeat='m in vm.movies'>{{m}}</li>"+
"</ol>";
return str;
}
}
}
</script>
</head>
<body>
<div ng-app="app" ng-controller="myctrl as vm" class="container">
<div class="page-header">
<h3>{{vm.title}}</h3>
</div>
<div class="row">
<div class="col-md-6">
<div class="page-header">
<h5>{{vm.actorName}}</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6" load-films></div>
</div>
</div>
</body>
的指令「加載影片」都有自己的適用範圍,
在該指令中的值來選擇框從傳遞封閉控制器「myctrl」中的父範圍變量。
工作例子是在鏈路現在 Example One
,一旦予改變指令「負載的膜」的範圍,以false
像
function loadFilms(){
return {
restrict : "EA",
controller : "childCtrl",
controllerAs : "vm",
scope : false,
template : function(tElem, tAttrs){
var str = "<select ng-model='vm.selectedYear' data-ng-options='y as y for y in year' " + "class='form-control' ng-change='vm.getMovies()'>" +
"<option value=''>Select</option>" +
"</select>" +
"<br />" +
"<ol class='slide-animate-container'>" +
" <li class='slide-animate' ng-repeat='m in vm.movies'>{{m}}</li>"+
"</ol>";
return str;
}
}
}
值vm.title
和父控制器的vm.actorName
不在UI中加載。
爲什麼?
理想vm.title
和vm.actorName
是在控制器「myCtrl」,那麼如何scope
屬性設置爲指令false
影響父控制變量,作爲 變量actorName
和title
連接到this
不$scope
。
使用{.., scope = false,..}
工作的例子是here
將範圍:false更改爲範圍:{} –