簡體   English   中英

從控制器Angularjs調用鏈接功能

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM