Javascript AngularJS:为什么可以';我不能在div元素中使用指令而不使用它吗?
在下面的AngularJS代码中,为什么我不能像Javascript AngularJS:为什么可以';我不能在div元素中使用指令而不使用它吗?,javascript,angularjs,Javascript,Angularjs,在下面的AngularJS代码中,为什么我不能像或那样使用评级指令? 阅读文档,这应该是可能的吗 我试着查看代码,看看哪里失败了,但我找不到原因。它工作得很好,就像 var starApp=angular.module('starApp',[]); 控制器('starCtrl',['$scope',函数($scope){ $scope.rating=0; $scope.ratings={ 当前:5, 最多:10 }; $scope.getSelectedRating=功能(评级){ 控制台日
或
那样使用评级指令?
阅读文档,这应该是可能的吗
我试着查看代码,看看哪里失败了,但我找不到原因。它工作得很好,就像
var starApp=angular.module('starApp',[]);
控制器('starCtrl',['$scope',函数($scope){
$scope.rating=0;
$scope.ratings={
当前:5,
最多:10
};
$scope.getSelectedRating=功能(评级){
控制台日志(额定值);
}
}]);
starApp.directive('rating',function(){
返回{//指令定义对象
restrict:'A',//指令将用作属性
模板:'
”,
作用域:{//指令作用域中的变量(从HTML传入)
ratingValue:“=”,//需要HTML中的指令中的对象
最大值:'=',
onRatingSelected:“&”
},
链接:功能(范围、要素、属性){
var updateStars=函数(){
scope.stars=[];
对于(变量i=0;i
将限制属性更改为:
restrict: 'EA'
E表示指令本身可以是标记(
)
A表示指令可以是HTML标记的属性(
)您的指令被定义为“A”,表示属性
(restrict: 'A', // directive will be used as an attribute)
您需要将“E”限制为元素,或将“AE”两者都限制为元素。因为您使用的是将restrict选项限制为
A
,所以您的指令仅限于属性,因此您不能对目录使用元素语法。为此,请使用E
restrict: 'EA', //no need to worry for its order like 'AE' or 'EA'
“限制”选项通常设置为:
'A' - only matches attribute name
'E' - only matches element name
'C' - only matches class name
这些限制可以根据需要进行组合:
'AEC' - matches either attribute or element or class name
有关详细信息:
还有指令类型
comment
,我们可以使用M
,但很少使用
'AEC' - matches either attribute or element or class name