Watch controller model value from inside directive

前端 未结 4 402
天命终不由人
天命终不由人 2020-12-13 04:57

I am trying to have angular watch the $viewValue of a controller from inside a directive.

fiddle: http://jsfiddle.net/dkrotts/TfTr5/5/

f         


        
4条回答
  •  情歌与酒
    2020-12-13 05:41

    Wanted to add: in 1.2.x, with isolated scope, the above wont work. http://jsfiddle.net/TfTr5/23/

    A workaround I came up with was using the fact that $watch also accepts a function, so you can access your controller using that.

    scope.$watch(
        function(){return controller.$viewValue},
        function(newVal, oldVal){
            //code
        }
    )
    

    Working fiddle: http://jsfiddle.net/TfTr5/24/

    If anyone has an alternative, I would gladly welcome it!

提交回复
热议问题