How to set angular controller object property value from directive in child scope

$parse will solve your problem.

<button ng-update1="inputdata.title">
app.directive('ngUpdate1', function($parse) {
    return function(scope, element, attrs) {
        var model = $parse(attrs.ngUpdate1);
        console.log(model(scope));  // logs "test"
        element.bind('click', function() {
           model.assign(scope, "Button 1");
           scope.$apply();
        });
    };
});

Fiddle

Whenever a directive does not use an isolate scope and you specify a scope property using an attribute, and you want to modify the value, use $parse.

If you don’t need to modify the value, you can use $eval instead:

console.log(scope.$eval(attrs.ngUpdate1));

Leave a Comment