I have a legacy page that has a <input type="hidden" id="myId" value="somenumber">
that is populated with Jquery (legacy code) asynchronously after the page loads for the first time. On this page I am trying to have an angularjs directive. The only problem is I am trying to watch on #myId value so I can use it for my directive but the value changes and triggers the watcher once and it is empty - it doesn't happen again when it actually gets a numercal value from jquery. What am I doing wrong? I tried with ng-value,ng-model but still I get undefined for those attempts. Also it is not possible to introduce routing etc to get the id from there.. Thanks
Here is my directive:
angular.module("myModule")
.directive("myDir", function () {
return {
restrict: 'A',
templateUrl: "template.html",
scope : {},
link: function (scope, element, attrs, ctrl) {
scope.$watch(function () { return $("#myId").val() }, function (newVal) { ctrl.myId= newVal});
},
controllerAs: 'myCtrl'
};
});