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);
});
}
}
});