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

前端 未结 2 1801
南笙
南笙 2020-12-02 11:46

I have have a directive inside an ng-repeater that should set a scope property. Please see the fiddle here: http://jsfiddle.net/paos/CSbRB/

The problem is that the s

2条回答
  •  旧时难觅i
    2020-12-02 12:29

    $parse will solve your problem.

    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));
    

提交回复
热议问题