I'm a 2 week old Angular noob, and I've been attempting my first ever directive for over a day now. :P I've read this and this, which seem good directives introductions and a bunch of Stackoverflow answers, but I can't get this working.
<div ng-app="App" ng-controller="Main"> <textarea caret caret-position="uiState.caretPosition"></textarea> {{uiState.caretPosition}} </div>
and
angular.module('App', []) .controller('Main', ['$scope', function ($scope) { $scope.uiState = {}; $scope.uiState.caretPosition = 0; }]) .directive('caret', function() { return { restrict: 'A', scope: {caretPosition: '='}, link: function(scope, element, attrs) { var $el = angular.element(element); $el.on('keyup', function() { scope.caretPosition = $el.caret(); }); } }; });
Fiddle is here. I'm basically trying to get the caret position within a textbox. I'm using this jQuery plugin in the fiddle (source of the .caret() method, which should just return a number).
My questions are
Expression 'undefined' used with directive 'caret' is non-assignable!
. I've read the doc and as far as I can tell I've followed instructions for how to fix to no avail.Thanks!
My solution was harder to find out here, but easier to implement. I had to change it to the equivalent of;
scope: {caretPosition: '=?'},
(Note that the question mark makes the attribute optional. Prior to 1.5 this apparently wasn't required.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With