0

I have view

<div ng-controller="DisplayController">
    <display-view style="width:{{model.width}}" value={{model.value}}</display-view>
</div>

Given my controller as

angularModule.controller("DisplayController",function($scope,$rootScope){
    $rootScope.model = DisplayModel;
    this.setInputValue = function(value){
        console.log($rootScope.model);
        $rootScope.model.value =  value;
        console.log($rootScope.model);
        $scope.model.value = $rootScope.model.value;
        $scope.$apply();
    }
});

Model as

var DisplayModel = {
    width:'193px',
    height:'25px',
    value:''
}

on some element click I'm calling the setInputValue function:

element.bind('click', function(event){
    console.log(event + '..event..');
    DispController.setInputValue(event.target.value);
});

display-view is

angularModule.directive('displayView',function(){

return {
    restrict: 'E',
    controller : "DisplayController",
    template: '<input type="text" style={{widthValue}} value={{value}}>',
    link:function(scope,element,attrs){
       scope.widthValue = attrs.style;
       scope.value = attrs.value;
    }
}

})

where the value in $rootScope is not getting updated to the view. How to resolve this issue.

devnull69
  • 16,402
  • 8
  • 50
  • 61
bvakiti
  • 3,243
  • 4
  • 17
  • 26
  • 2
    `display-view` seems to be a directive. Can you share the code corresponding to it ? It'll be better if you could make a plunkr to demonstrate your issue – nalinc Jun 24 '15 at 05:39
  • included the display-view code – bvakiti Jun 24 '15 at 05:49
  • Where does `DispController` come from? – devnull69 Jun 24 '15 at 05:54
  • i have specified that in link attribute of other directive. link: function(scope,element,attrs,DispController){ scope.value = attrs.value; scope.className = attrs.class; element.bind('click',function(event){ console.log(event + '..event..'); DispController.setInputValue(event.target.value); }); – bvakiti Jun 24 '15 at 05:55
  • Are you sure `value={{model.value}}` shouldn't be `value="{{model.value}}"`? – godfrzero Jun 24 '15 at 06:38

1 Answers1

0

Here we will have to include replace:true. This will append the value and style to the input element in the display-view directive.

angularModule.directive('displayView',function(){
  return {
  restrict: 'E',
  replace:true,
  template: '<input type="text">'
}
})

Other way to display is by isolating the scope

angularModule.directive('displayView',function(){
return {
restrict: 'E',
scope:{
widthValue: "@",
value : "@"
},
template: '<input type="text" style={{widthValue}} value={{value}}>',
link:function(scope,element,attrs){

}
}
})
bvakiti
  • 3,243
  • 4
  • 17
  • 26