Reputation: 4946
I have a directive that I am trying to set the ng-init
to a variable set with $scope
<select ng-init="safe.id=currentSafe" ng-options="safe as safe.name for safe in safes track by safe.id" ng-model="safe" ng-change="getSafeUrl(safe.id)"></select>
In the link
function on the directive I have:
$scope.currentSafe = '72824ca7-99ab-4f16-a56c-3c98328c73fd';
This is not working. However if I change the template to read:
<select ng-init="safe.id='72824ca7-99ab-4f16-a56c-3c98328c73fd'" ng-options="safe as safe.name for safe in safes track by safe.id" ng-model="safe" ng-change="getSafeUrl(safe.id)"></select>
Why am I not able to use a string from $scope
but I can use it directly?
Upvotes: 3
Views: 3392
Reputation: 123739
1) You should not use select as with track by, they are not designed to work together. See documentation for details.
2) Do not use ng-init for initializing property which should be done on the controller, ng-inited expressions are not watched, set $scope.safe.id
on the link/controller function of the directive itself. In your case you might be setting currentSafe
asynchronously but ng-init would have evaluated it already by then.
<select ng-options="safe.name for safe in safes track by safe.id"
ng-model="safe"
ng-change="getSafeUrl(safe.id)"></select>
and set:
$scope.safe = {id:whateverid}
Upvotes: 7