我有一个例子angularJS
<div ng-controller="testCtrl">
<test color1="color1" updateFn="updateFn()"></test>
</div>
<script>
angular.module('dr', [])
.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function() {
alert('123');
}
})
.directive('test', function() {
return {
restrict: 'E',
scope: {color1: '=',
updateFn: '&'},
template: "<button ng-click='updateFn()'>Click</button>",
replace: true,
link: function(scope, elm, attrs) {
}
}
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想当我点击按钮时,会出现警告框,但没有任何显示.
谁能帮我?
Alw*_*ner 239
要从隔离范围指令内部调用父作用域中的控制器函数,请使用dash-separatedHTML中的属性名称,如OP所述.
此外,如果要向函数发送参数,请通过传递对象来调用该函数:
<test color1="color1" update-fn="updateFn(msg)"></test>
Run Code Online (Sandbox Code Playgroud)
var app = angular.module('dr', []);
app.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function(msg) {
alert(msg);
}
});
app.directive('test', function() {
return {
restrict: 'E',
scope: {
color1: '=',
updateFn: '&'
},
// object is passed while making the call
template: "<button ng-click='updateFn({msg : \"Hello World!\"})'>
Click</button>",
replace: true,
link: function(scope, elm, attrs) {
}
}
});
Run Code Online (Sandbox Code Playgroud)
ste*_*eve 155
也许我遗漏了一些东西,但是虽然其他解决方案确实调用了父范围函数,但是没有能力从指令代码传递参数,这是因为例如使用固定参数update-fn调用.稍微改变允许指令传递参数,我认为这更有用.updateFn(){msg: "Hello World"}
<test color1="color1" update-fn="updateFn"></test>
Run Code Online (Sandbox Code Playgroud)
请注意,HTML正在传递函数引用,即没有()括号.
JS
var app = angular.module('dr', []);
app.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function(msg) {
alert(msg);
}
});
app.directive('test', function() {
return {
restrict: 'E',
scope: {
color1: '=',
updateFn: '&'
},
// object is passed while making the call
template: "<button ng-click='callUpdate()'>
Click</button>",
replace: true,
link: function(scope, elm, attrs) {
scope.callUpdate = function() {
scope.updateFn()("Directive Args");
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
所以在上面,HTML调用本地作用域callUpdate函数,然后从父作用域"获取"updateFn并使用指令可以生成的参数调用返回的函数.
Ofe*_*gev 38
在'test'指令Html标记中,函数的属性名称不应该是camelCased,而是基于破折号.
所以 - 而不是:
<test color1="color1" updateFn="updateFn()"></test>
Run Code Online (Sandbox Code Playgroud)
写:
<test color1="color1" update-fn="updateFn()"></test>
Run Code Online (Sandbox Code Playgroud)
这是angular的方式来区分指令属性(例如update-fn函数)和函数之间的区别.
如何通过双向绑定传递控制器功能?然后你可以在指令中使用它,就像在常规模板中一样(为了简单起见,我剥离了不相关的部分):
<div ng-controller="testCtrl">
<!-- pass the function with no arguments -->
<test color1="color1" update-fn="updateFn"></test>
</div>
<script>
angular.module('dr', [])
.controller("testCtrl", function($scope) {
$scope.updateFn = function(msg) {
alert(msg);
}
})
.directive('test', function() {
return {
scope: {
updateFn: '=' // '=' bidirectional binding
},
template: "<button ng-click='updateFn(1337)'>Click</button>"
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
我降落在这个问题,因为我尝试了上面的方法befire,但不知怎的,它没有用.现在它完美无缺.
小智 5
使用破折号和小写字母表示属性名称(就像说的其他答案一样):
<test color1="color1" update-fn="updateFn()"></test>
Run Code Online (Sandbox Code Playgroud)
并在指令范围内使用“ =”代替“&”:
scope: { updateFn: '='}
Run Code Online (Sandbox Code Playgroud)
然后,您可以像其他任何函数一样使用updateFn:
<button ng-click='updateFn()'>Click</button>
Run Code Online (Sandbox Code Playgroud)
你去!
| 归档时间: |
|
| 查看次数: |
136753 次 |
| 最近记录: |