EDIT As correctly noted in the comments, using this with ng-change requires a preliminary preview of the "dummy" ng. However, it should be noted that, apparently, with 1.3, the required parameters were provided by the framework. Please check out the https://stackoverflow.com/a/166338/ below! / EDIT
Just in case, when you are like me, tripping over a simple case, having a more difficult task, this is the solution I came up with for dynamically linking arbitrary expressions to ng-model: http://plnkr.co/edit/ccdJTm0zBnqjntEQfAfx?p= preview
Method: I created a dynamicModel directive that takes a standard angular expression, evaluates it, and associates the result with the scope using ng-model and $ compile.
var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.data = {}; $scope.testvalue = 'data.foo'; $scope.eval = $scope.$eval; }); var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.data = {}; $scope.testvalue = 'data.foo'; $scope.eval = $scope.$eval; }); app.directive('dynamicModel', ['$compile', function ($compile) { return { 'link': function(scope, element, attrs) { scope.$watch(attrs.dynamicModel, function(dynamicModel) { if (attrs.ngModel == dynamicModel || !dynamicModel) return; element.attr('ng-model', dynamicModel); if (dynamicModel == '') { element.removeAttr('ng-model'); }
The use is simply a dynamic model = "angular expression", where the expression of the angular expression leads to a string that is used as the expression for the ng model.
I hope this saves someone the headache of having to come up with this solution.
Regards, Justus
Justus Wingert Jul 02 '14 at 8:00 2014-07-02 08:00
source share