无法获取独立范围的值
Not able to fetch value of isolated scope
无法获取独立范围 "check" 的值。出于测试目的,我将索引值传递给 "check"。我必须在 ng-repeat 完成时添加动画,但是 "scope.$last" 也给出了未定义的值。我不会发现我的代码语法或逻辑有什么问题。
angular.module('myApp',[])
.directive('track',function(){
return{
restrict:'E',
scope: { check : "="},
template:`<div class="routeTable"><table class="table" id="road">
<tr ng-repeat="level in levels">
<td ng-repeat="lane in lanes" check = {{$index}}></td>
</tr>
</table></div>`,
controller: function($scope){
$scope.levels = [1,2];
$scope.lanes= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
},
link: function(scope,elem,attr){
console.log(scope.$last);
console.log(attr.check);
}
}
})
.routeTable table tr td{
border: 1px solid #000000 !important;
height:30px;
background-color:#CACDD0;
width:30px;
}
.routeTable{
padding:10px;
width:500px;
}
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<track></track>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="track.js"></script>
</body>
</html>
我想你误会了什么。 scope
变量传递仅适用于指令的相同标记。在下面的示例中,我定义了一个新指令 d1
,check
属性将正确传递给它。
angular.module('myApp',[])
.directive('track',function(){
return{
restrict:'E',
template:`<div class="routeTable"><table class="table" id="road">
<tr ng-repeat="level in levels">
<td ng-repeat="lane in lanes" d1 check = "$index"></td>
</tr>
</table></div>`,
controller: function($scope){
$scope.levels = [1,2];
$scope.lanes= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
}
}
})
.directive('d1', function(){
return {
scope: { check : "="},
link: function(scope,elem,attr){
console.log(scope.check);
}
}
})
.routeTable table tr td{
border: 1px solid #000000 !important;
height:30px;
background-color:#CACDD0;
width:30px;
}
.routeTable{
padding:10px;
width:500px;
}
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<track></track>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="track.js"></script>
</body>
</html>
无法获取独立范围 "check" 的值。出于测试目的,我将索引值传递给 "check"。我必须在 ng-repeat 完成时添加动画,但是 "scope.$last" 也给出了未定义的值。我不会发现我的代码语法或逻辑有什么问题。
angular.module('myApp',[])
.directive('track',function(){
return{
restrict:'E',
scope: { check : "="},
template:`<div class="routeTable"><table class="table" id="road">
<tr ng-repeat="level in levels">
<td ng-repeat="lane in lanes" check = {{$index}}></td>
</tr>
</table></div>`,
controller: function($scope){
$scope.levels = [1,2];
$scope.lanes= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
},
link: function(scope,elem,attr){
console.log(scope.$last);
console.log(attr.check);
}
}
})
.routeTable table tr td{
border: 1px solid #000000 !important;
height:30px;
background-color:#CACDD0;
width:30px;
}
.routeTable{
padding:10px;
width:500px;
}
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<track></track>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="track.js"></script>
</body>
</html>
我想你误会了什么。 scope
变量传递仅适用于指令的相同标记。在下面的示例中,我定义了一个新指令 d1
,check
属性将正确传递给它。
angular.module('myApp',[])
.directive('track',function(){
return{
restrict:'E',
template:`<div class="routeTable"><table class="table" id="road">
<tr ng-repeat="level in levels">
<td ng-repeat="lane in lanes" d1 check = "$index"></td>
</tr>
</table></div>`,
controller: function($scope){
$scope.levels = [1,2];
$scope.lanes= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
}
}
})
.directive('d1', function(){
return {
scope: { check : "="},
link: function(scope,elem,attr){
console.log(scope.check);
}
}
})
.routeTable table tr td{
border: 1px solid #000000 !important;
height:30px;
background-color:#CACDD0;
width:30px;
}
.routeTable{
padding:10px;
width:500px;
}
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<track></track>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<script src="track.js"></script>
</body>
</html>