be-codified
be-codified

Reputation: 6144

AngularJS - passing variable to scope (from directive to use it in controller) not working

I would ask for some help and explanation on the given issue. It seems like I can not pass variable to scope from directive to use it in controller, variable is called caret_position. Please see below code.

Controller

var myApp = angular.module('myApp', []);

myApp.controller('Composer', function Composer($scope, $http) {

    // getting snippets json data
    $http.get('data/snippets/default.json').success(function(data) {
        $scope.snippets = data;
        $scope.snippets.count = data.length;
    });

    // adding snippet to composed text
    $scope.composed_text = '';
    $scope.snippet_insert = function() {
        var snippet = this.item.content;

        console.log($scope.caret_position); // stackoverflow.com note: this is not working
    };
});

Directive:

myApp.directive('caretPosition', function() {
    return {
        link: function(scope, element, attrs) {
            element.bind('keyup click', function(e){
                var caret_position = element[0].selectionStart;
                scope.caret_position = caret_position; // stackoverflow.com note: this is not working
                scope.$apply(); // stackoverflow.com note: not working as well
                console.log('my current position: ' + caret_position);
            });
        }
    }
});

Upvotes: 0

Views: 2744

Answers (3)

user2266928
user2266928

Reputation: 475

make the service in
var myApp = angular.module('mean.mapping2');
myApp.factory('SharedService', function() {
    return {
        caretInfo: {
            position: 0
        }
    };
});

inject service in controller
angular.module('mean.mapping2').controller('mapProperties', ['$scope', 'Global', 'Mapping2', '$http', '$stateParams','SharedService',

console.log( $scope.caret = SharedService.caretInfo);//controller part for calling services

in directive use as 
 .directive('repeatEvents', function ($timeout,SharedService) {

 .directive('setupConnections', function ($location,SharedService) {//i have two directive


use this in directive
 var caret_position = items;
                            SharedService.position = caret_position;
                            console.log('Caret current position: ' + SharedService.position);

Upvotes: 0

Fedaykin
Fedaykin

Reputation: 4552

The recommended way to share data between directives and controllers is by using a Service, you can create one by using the factory method:

var app = angular.module('plunker', []);

app.factory('SharedService', function() {
  return {
    sharedObject: {
      value: '',
      value2: ''
    }
  };
});

And then you may inject your SharedService on both your directive and controller.

Here is a more detailed example about sharing data between controllers and directives:

http://plnkr.co/edit/Q1VdKJP2tpvqqJL1LF6m?p=preview

Hope that helps

Update: I just edited your example to use that concept and it is working, take a look: http://plnkr.co/edit/2pUODHCd9qTRcQTiziC2?p=preview

Upvotes: 3

Rahul Ravindran
Rahul Ravindran

Reputation: 318

You would need to use a 2 way binding variable on your directive with isolate scope. That is the way a directive is able to update a scope property of an enclosing controller.you can do this by specifying scope:{catetproperty:'='} on your directive definition object

Upvotes: 0

Related Questions