Angularjs 将控制器作用域绑定到bindToController的指令,作为引用不起作用
我是angular bindToController的新手,当我试图将控制器作用域映射为一个不起作用的指令的引用时Angularjs 将控制器作用域绑定到bindToController的指令,作为引用不起作用,angularjs,Angularjs,我是angular bindToController的新手,当我试图将控制器作用域映射为一个不起作用的指令的引用时 Example AS: app.controller('test',function($scope){ $scope.reftodir = $scope; $scope.test = 'someconte'; }) in html referenced it as : <test-directive testscope =
Example AS:
app.controller('test',function($scope){
$scope.reftodir = $scope;
$scope.test = 'someconte';
})
in html referenced it as :
<test-directive testscope = 'reftodir' iso-scope="test"/>
app.directive('testDirective',function(){
return{
scope:{
isoScope: '='
},
bindToController: {
testscope:'='
},
}
})
Suppose i have a function in controller as test_function, i need to call that function from directive with the help controller scope available from bindToController variable such as testscope.test_function().
but test_function is not available. Please provide any suggestions, Thanks in advance friends.
示例如下:
应用程序控制器('测试',功能($范围){
$scope.reftodir=$scope;
$scope.test='someconte';
})
在html中,将其引用为:
app.directive('testDirective',function(){
返回{
范围:{
等示波器:'='
},
bindToController:{
testscope:“=”
},
}
})
假设我在控制器中有一个函数作为test_函数,我需要从指令中调用该函数,并使用bindToController变量(如testscope.test_function())中提供的帮助控制器作用域。
但test_功能不可用。请提供任何建议,提前感谢朋友们。
您的指令对象应该如下所示:
return {
controller: 'test',
bindToController: true,
controllerAs: 'vm'
scope: {
testScope: '='
}
}
app.controller('test',function(){
this.testScope //scope is available here
})
然后在控制器中,您应该能够看到如下范围:
return {
controller: 'test',
bindToController: true,
controllerAs: 'vm'
scope: {
testScope: '='
}
}
app.controller('test',function(){
this.testScope //scope is available here
})
请注意,如果我们绑定到控制器,我们将不再需要$scope,并且
此
部分指向控制器本身我在指令中有一些独立的作用域变量,因此我需要控制器作用域的引用来执行某些操作,而我正在尝试设置bindToController=true,独立作用域变量被覆盖。你能提供一个例子吗,对不起,我不明白。testScope是undefined@Richard您是否将任何变量从用户代码传递到指令的属性测试范围
?为什么要这样做,用例是什么?我正在尝试为我的应用程序构建一个表组件(作为可包含在任何应用程序中的模块),对于依赖关系计算,它已命中相应控制器的函数。