angularjs - How to pass a function variable from a controller to a directive link? -
i trying evaluate variable through directive. how can pass variable found inside function controller directive link? able pass globally if variable set inside function says undefined. tried rootscope know avail.
controller
$scope.checkforcall = function(){ $http({ url: $locationprovider + 'broadcast_call', method: "get" }).success(function (data){ if(data != 'none'){ $scope.ccards = data.broadcast; $scope.hasdata = 1; } else { $scope.hasdata = 0; } }); }
my directive
app.directive('cards', function($timeout,$interval){ return{ restrict: 'eac', template: '<h1>not found</h1>', link: function($scope){ if($scope.hasdata == 1){ // undefine console.log("has data") }else{ console.log("not found") } } }; });
because use data $http .success, asynchronous change. means scope.hasdata
undefined @ first, change 0 or 1 @ later time.
for case can setup watcher in directive can when detect changes.
link: function (scope, element, attrs){ // fix line suggested other poster scope.$watch(function(){ return scope.hasdata; }, function() { if(scope.hasdata == 1){ console.log("has data"); } else if(scope.hasdata == 0){ console.log("not found"); } else { console.log("not ready yet"); } }); }
Comments
Post a Comment