[英]trouble passing argument from directive to controller
Im having issues with passing an argument from a directive to a controller. 我在将参数从指令传递到控制器时遇到问题。 The argument "compoundID" keeps coming back undefined. 自变量“ compoundID”一直未定义。 Im using angular js. 我正在使用角度js。 I plan to have more then one of the directives per page. 我计划每页有多个指令之一。
JS: JS:
angular.module('ireg').directive('compound', function () {
return {
restrict:'E',
scope:{
compoundID:'='
},
templateUrl: '/ireg/components/compound/compound.html'
};
});
angular.module('ireg').controller("compoundController",['$scope','$attrs','compoundService', function($scope,$attrs,compoundService ){
var vm = this;
vm.compoundID = $attrs.compoundID;
console.log($attrs);
}]);
HTML: HTML:
<div class = "compound-view" ng-controller = "compoundViewController as controll" >
<compound compoundID="{{controll.compoundID}}"></compound>{{controll.compoundID}}
<div = "studies" ng-repeat="study in controll.studies">
<studie studyID="{{study.ID}}"></studie>
</div>
<cro croID= "{{croID}}"></cro>
If you're declaring scope like this: 如果您要声明这样的范围:
scope:{
compoundID:'='
},
You're simply telling that the compoundID
attribute should be treated as a model to be automatically parsed by Angular. 您只是在说应将compoundID
属性视为要由Angular自动解析的模型。 You should use the model directly, so instead of: 您应该直接使用模型,所以不要:
<compound compoundID="{{controll.compoundID}}">
write: 写:
<compound compoundID="controll.compoundID">
If you wanted to write <compound compoundID="{{controll.compoundID}}">
, your scope should be declared like this instead: 如果您想编写<compound compoundID="{{controll.compoundID}}">
,则应这样声明您的范围:
scope:{
compoundID:'@'
},
Also mind that Angular translated aSampleAttribute
("camel case") in your directive's definition to a-sample-attribute
(em… "kebab case"??) to be used in html. 另外请注意,Angular将指令定义中a-sample-attribute
aSampleAttribute
(“驼峰大小写”)转换为要在html中使用的a-sample-attribute
(例如:“ kebab case” ??)。 So I think that if you have compoundID
in directive you may have to write <compound compound-id="………">
. 因此,我认为如果指令中包含compoundID
,则可能必须编写<compound compound-id="………">
。
I know it's nasty, so I'd suggest you use sth like compoundId
=> compound-id
instead. 我知道这很讨厌,因此建议您使用诸如compoundId
=> compound-id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.