如何将URL从控制器传递到angularjs中的Directive

时间:2015-09-08 19:28:22

标签: angularjs

angular.module('finalApp').controller('GetAgentDetailsCtrl', function ($scope,$rootScope){
    var vm=this;
    vm.url='api/agent/getAgentDetails';
});


'use strict';
angular.module('finalApp').directive('homeHeader',['HttpService',function(HttpService){
    return{
        restrict:'E',
        templateUrl:'views/directives/header/header.html',
        controller: 'GetAgentDetailsCtrl',
        controllerAs: 'GetAgentDetlCtrl',
        link: function ($scope) {
            HttpService.get(GetAgentDetlCtrl.url).success(function (data) {
                $scope.GetAgentDetlCtrl.defaultData = data;
                console.log($scope.GetAgentDetlCtrl.defaultData);
            });
        }
    };
}]);


<header ng-controller="GetAgentDetlCtrl">     
    {{GetAgentDetlCtrl.data}}
</header>

<main>
<home-header></home-header>
</main>

1 个答案:

答案 0 :(得分:0)

angular.module('finalApp').controller('GetAgentDetailsCtrl', function ($scope,$rootScope){
    var vm=this;
    vm.url='api/agent/getAgentDetails';
});


'use strict';
angular.module('finalApp').directive('homeHeader',['HttpService',function(HttpService){
    return{
        restrict:'E',
        templateUrl:'views/directives/header/header.html',
        controller: 'GetAgentDetailsCtrl',
        controllerAs: 'GetAgentDetlCtrl',
        scope: {
         url : '='
        }
        link: function ($scope) {
            HttpService.get($scope.url).success(function (data) {
                $scope.GetAgentDetlCtrl.defaultData = data;
                console.log($scope.GetAgentDetlCtrl.defaultData);
            });
        }
    };
}]);


<header ng-controller="GetAgentDetlCtrl">     
    {{GetAgentDetlCtrl.data}}
</header>

<main>
<home-header url="http://yoursite.blah"></home-header>
</main>