Javascript 将不同的数组变量传递给指令
在一个控制器中,我定义了两个不同的数组,需要将不同的数组传递给指令,但只传递相同的项数组 实际上,这只是一些片段。在我的实际代码中,我在另一个指令中使用这个指令 这是造成问题的原因还是其他原因Javascript 将不同的数组变量传递给指令,javascript,angularjs,Javascript,Angularjs,在一个控制器中,我定义了两个不同的数组,需要将不同的数组传递给指令,但只传递相同的项数组 实际上,这只是一些片段。在我的实际代码中,我在另一个指令中使用这个指令 这是造成问题的原因还是其他原因 <div my-sticky tags="items" template_name="test2.html"></div> <div my-sticky tags="kititems" template_name="test2.html"></div>
<div my-sticky tags="items" template_name="test2.html"></div>
<div my-sticky tags="kititems" template_name="test2.html"></div>
试试这个
在您的templateURL(test2.html)中,应该是这样的:
<div ng-repeat="tag in tags"> {{tag.id}} {{tag.text}} </div>
角模
var app = angular.module("myApp",[]);
app.controller('MyCtrl', function($scope) {
$scope.items = [
{ id: 18, text: '1' },
{ id: 28, text: '2' },
{ id: 38, text: '3' }
];
$scope.kititems = [
{ id: 0, text: '001' },
{ id: 028, text: '002' },
{ id: 038, text: '003' }
];
});
app.directive("mySticky", function($http, $compile) {
return {
restrict : "A",
scope : {
templateName: "@",
tags: "=",
},
template :'<div ng-repeat="tag in tags"> {{tag.id}} {{tag.text}} </div>',
controller : function($http, $scope, $element, $sce, $templateCache, $compile, $attrs) {
$scope.drawerStyle = {left: '140px'};
// console.log($scope);
//$scope.CommonArray=$attrs.tags;
},
replace : false,
transclude : false,
link : function(scope, element, attrs) {
// change element just to show we can
}
};
});
var-app=angular.module(“myApp”,[]);
应用程序控制器('MyCtrl',函数($scope){
$scope.items=[
{id:18,文本:'1'},
{id:28,文本:'2'},
{id:38,文本:'3'}
];
$scope.kititems=[
{id:0,文本:'001'},
{id:028,文本:'002'},
{id:038,文本:'003'}
];
});
指令(“mySticky”,函数($http,$compile){
返回{
限制:“A”,
范围:{
templateName:“@”,
标签:“=”,
},
模板:{{tag.id}{{tag.text}},
控制器:函数($http、$scope、$element、$sce、$templateCache、$compile、$attrs){
$scope.drawerStyle={left:'140px'};
//console.log($scope);
//$scope.CommonArray=$attrs.tags;
},
替换:false,
排除:错误,
链接:函数(范围、元素、属性){
//改变元素只是为了显示我们可以
}
};
});
模板中可能有错误(使用相同的范围变量)。向我们展示模板。您在展示的代码中是否遇到问题?似乎对我有用。@tasseKATT是的,数组变量未定义?你试过什么?
<div ng-controller="MyCtrl">
MyCtrl
<div my-sticky tags="items"></div>
<hr/>
<div my-sticky tags="kititems"></div>
</div>
var app = angular.module("myApp",[]);
app.controller('MyCtrl', function($scope) {
$scope.items = [
{ id: 18, text: '1' },
{ id: 28, text: '2' },
{ id: 38, text: '3' }
];
$scope.kititems = [
{ id: 0, text: '001' },
{ id: 028, text: '002' },
{ id: 038, text: '003' }
];
});
app.directive("mySticky", function($http, $compile) {
return {
restrict : "A",
scope : {
templateName: "@",
tags: "=",
},
template :'<div ng-repeat="tag in tags"> {{tag.id}} {{tag.text}} </div>',
controller : function($http, $scope, $element, $sce, $templateCache, $compile, $attrs) {
$scope.drawerStyle = {left: '140px'};
// console.log($scope);
//$scope.CommonArray=$attrs.tags;
},
replace : false,
transclude : false,
link : function(scope, element, attrs) {
// change element just to show we can
}
};
});