2

JavaScript function:

 var API = (function(){
        return {
            function invokeDirective(){
                invvoke();
                $scope.setItem() // to directive
            }       
        }
    });

Directive:

angular.module('sampleComponents', ['$strap.directives']).directive('titlebar',
function($filter,$timeout)
{
    return {
        restrict: 'AE',
        replace: true,
        scope: true,
        transclude: true,
        template: '<div class="searchBar r  etc ........',
        controller: function($scope,$http,$timeout)
        {
            $scope.setItem = function(){
                // want to invoke this function from api.js -
            }
        }
        link: function(scope, element, attrs){
            // etc:
        }

});

how can I invoke $scope.setItem from api.js? Is it possible? Please suggest.

(currently, I am using timer, but that is creating some performance issue )

1
  • 5
    API.js should itself be an Angular service, injected into your directive as a dependency. You should then expose the service's calls via an external API if required. Commented Nov 26, 2014 at 8:45

1 Answer 1

1

Yes, you can do that with a very hacky way.

Demo code:

//This is too get scope outside of angular controller/service/directive
//with a hacky way to do that.

var $injector =angular.injector(['ng','MyApp']);
var html = "<div titlebar=''></div>";
$injector.invoke(function($compile, $rootScope){
    var $scope = $rootScope.$new();
    var result= $compile(html)($scope);

    var cScope = result.scope(); // This is scope in directive controller. :  )
    //Do anything you want with this scope .
});

This is Jsfiddle.

Happy coding. : )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.