Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 即使根作用域具有该值,也不会更新作用域值_Angularjs_Angularjs Scope_Angularjs Rootscope - Fatal编程技术网

Angularjs 即使根作用域具有该值,也不会更新作用域值

Angularjs 即使根作用域具有该值,也不会更新作用域值,angularjs,angularjs-scope,angularjs-rootscope,Angularjs,Angularjs Scope,Angularjs Rootscope,我有自己的看法 <div ng-controller="DisplayController"> <display-view style="width:{{model.width}}" value={{model.value}}</display-view> </div> 模型为 var DisplayModel = { width:'193px', height:'25px', value:'' } 在某个元素上单击我

我有自己的看法

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

var DisplayModel = {
    width:'193px',
    height:'25px',
    value:''
}
在某个元素上单击我正在调用setInputValue函数:

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

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

})
angularModule.directive('displayView',function(){
返回{
限制:'E',
控制器:“显示控制器”,
模板:“”,
链接:函数(范围、元素、属性){
scope.widthValue=attrs.style;
scope.value=attrs.value;
}
}
})
其中,$rootScope中的值未更新到视图。
如何解决此问题。

这里我们必须包括replace:true。这将把值和样式附加到display view指令中的输入元素

angularModule.directive('displayView',function(){
  return {
  restrict: 'E',
  replace:true,
  template: '<input type="text">'
}
})
angularModule.directive('displayView',function(){
返回{
限制:'E',
替换:正确,
模板:“”
}
})
另一种显示方式是隔离作用域

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

}
}
})
angularModule.directive('displayView',function(){
返回{
限制:'E',
范围:{
宽度值:“@”,
值:“@”
},
模板:“”,
链接:函数(范围、元素、属性){
}
}
})

显示视图
似乎是一个指令。你能分享对应的代码吗?如果您可以制作一个plunkr来演示您的问题,包括显示视图代码,
DispController
来自何处?我已经在其他指令的link属性中指定了它。链接:函数(scope,element,attrs,DispController){scope.value=attrs.value;scope.className=attrs.class;element.bind('click',函数(event){console.log(event+'..event..);DispController.setInputValue(event.target.value);});您确定
值={{{model.value}
不应该是
value=“{model.value}
angularModule.directive('displayView',function(){
return {
restrict: 'E',
scope:{
widthValue: "@",
value : "@"
},
template: '<input type="text" style={{widthValue}} value={{value}}>',
link:function(scope,element,attrs){

}
}
})