我对Angularjs中的这个指令很新。
在通过指令范围时,我发现它们可以用三种形式声明,scope: false(shared scope)
,scope: true(inherited scope)
,scope: {}(isolated scope)
。
.directive('smsDownloadSampleFile',function() {
return {
restrict: 'E',
scope: false,
template: '<input type="button" class="btn cur-p btn-info pull-right" value="Download Sample Upload File" ng-click="getSampleFile()">',
link: function(scope,element,attr) {
scope.getSampleFile=function() {
// console.log("Getting sample file on");
}
DS=$scope;
}
}
})
所以我创建了这个指令,但我无法在父作用域中访问此方法getSampleFile
。据我所知,当scope=false
然后它没有创建一个新的范围,而是它共享父范围。如果是这样,那么为什么我无法在父范围内访问该方法。
请帮助我清除这些疑问并学习新的东西。
使用scope: false
,指令接收与父节点相同的精确范围对象。这样,当您执行以下操作时,您将覆盖父项上的函数:
scope.getSampleFile=function() { ... }
通常建议使用隔离范围,因为您必须明确地将数据传递到指令中,并且不能覆盖父级(除非您不愿意这样做)。