[英]Call link function from controller Angularjs
我想在控制器上执行ajax请求后,在指令的link
函数内声明一个函数。 因此,我想通过链接功能与控制器进行通讯。 这是我的控制器代码:
.controller('productsCtrl', ['$scope', '$location', '$http', function ($scope, $location, $http) {
this.courseSeriesId = $location.search().courseSeriesId;
//FUNCTION: get data from products to show them
this.getCourseSeriesProducts = function(){
$http({
method: 'post',
url: "xxx",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
functionName:'xxx',
courseSeriesId: this.courseSeriesId}
})
.success(function(response) {
---CODE
CALL welcome() function in LINK?
---CODE
}.bind(this))
.error(function(data){
alert('ERROR: ' + data);
});
}
这是我的指令代码:
.directive('nlProducts', ['$location', function($location) {
return {
restrict: 'E',
templateUrl: function(elem, attr){
var type = '';
switch($location.search().courseSeriesId){
case 'en-efw':
type = 'tableview';break;
}
return 'xxx/nl-products-'+ type+'.php';
},
//control DOM elements
link:
function welcome() {
element.text('hi!');
}
};
}]);
我知道该link
是在编译后调用的,但是在那一刻,ajax请求尚未完成。 我该怎么做?
谢谢。
你不能直接。 您可以在控制器中将事件与$ broadcast一起使用,在指令中将$ on与事件一起使用。 看到这个答案
或者您可以在指令中使用$ watch在控制器设置的变量中查找更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.