Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 为什么指令模板中的ng模型在控制器更改值时不更新_Angularjs_Angularjs Directive_Directive - Fatal编程技术网

Angularjs 为什么指令模板中的ng模型在控制器更改值时不更新

Angularjs 为什么指令模板中的ng模型在控制器更改值时不更新,angularjs,angularjs-directive,directive,Angularjs,Angularjs Directive,Directive,我正在使用一个指令,从双向绑定开始,我正在更改一个日期对象变量,该变量最初传递给该指令 但是当我对日期变量做一些更改时,比如 $scope.valueee = 1; $scope.press = function(){ $scope.searchterm.setHours($scope.valueee++, 0, 0, 0); if(!$scope.$$phase)$scope.$apply() } 但它不会在指令内部的模板视图中使用ng模型更新视图 'ng-model="term

我正在使用一个指令,从双向绑定开始,我正在更改一个日期对象变量,该变量最初传递给该指令

但是当我对日期变量做一些更改时,比如

$scope.valueee = 1;
$scope.press = function(){
    $scope.searchterm.setHours($scope.valueee++, 0, 0, 0);
  if(!$scope.$$phase)$scope.$apply()
}
但它不会在指令内部的模板视图中使用ng模型更新视图

'ng-model="term"'
下面是代码示例


我认为您在直接绑定到原语时遇到了这个问题:

重点矿山

作用域继承通常是直接的,您甚至不需要知道它正在发生。。。直到您尝试从子作用域内部将双向数据绑定(即表单元素、ng模型)到父作用域上定义的基元(例如数字、字符串、布尔值)为止。它不像大多数人期望的那样工作。发生的情况是,子作用域获取自己的属性,该属性隐藏/隐藏同名的父属性。这不是AngularJS正在做的事情——这是JavaScript原型继承的工作方式。新的AngularJS开发人员通常没有意识到ng repeat、ng switch、ng view、ng include和ng if都会创建新的子作用域,因此当涉及到这些指令时,问题往往会出现。(有关问题的快速说明,请参阅。)

通过遵循“最佳实践”,可以很容易地避免原语的这个问题–观看3分钟。Misko演示了ng开关的基本绑定问题

直接显示您的问题(来源如下):

javascript:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  
  /*
  ng-repeat generates new scopes which will be child scopes of the scope within
  which they are generated. In other words, this scope is the parent scope for
  the child scopes generated by the ng-repeat in this example. Child scopes
  inherit things from their parent's scope.
  */

  // The initial main image 
  var initialImg = "http://3.bp.blogspot.com/-z8kzafZYkfQ/UERf6IbjJJI/AAAAAAAAALE/qaAxqqawXpM/s1600/Cat+Pictures+1.jpg";
  
  /*
  A primitive holding the URL for the main image
  
  This scope's child scopes will "shadow" this primitive, which basically means
  they'll get their own copy that is initialy the same value. The child scopes
  can only see their own copy though, so modifying the value in the child scope
  does not affect the value in the parent scope.
  */
  $scope.mainImgUrl = initialImg;
  
  /*
  An object holding the URL for the main image
  
  This scope's child scopes will NOT get their own copy of this object.
  Referencing main or main.imgUrl in the child scope will reference this object
  on this scope (unless the child scope explicitly define its own "mainImg" object.)
  */
  $scope.mainImg = { url: initialImg };
  
  // Our 'thumbnail' images
  $scope.images = [
      "http://happy.fm/wp-content/uploads/2011/10/random-owl.jpg",
      "http://www.superhumor.com/emoticonos/8761.gif"
  ];
  
});
html:


安古拉斯普朗克
文件。写(“”);
ng在父范围上的ng重复设置值内单击

举例说明原型继承的细微差别。看见
和
.

使用原语: 这是主映像的父范围

$scope.mainImgUrl=={{mainImgUrl}

使用ng repeat生成的拇指,使用ng click setting$scope.mainImgUrl(单击拇指查看发生了什么):

这是ng repeat生成的子作用域

$scope.mainImgUrl=={{mainImgUrl}

使用对象: 这是主映像的父范围

$scope.mainImg.url=={{mainImg.url}

使用ng repeat生成的拇指,使用ng click设置$scope.mainImg.url(单击拇指查看发生了什么):

这是ng repeat生成的子作用域

$scope.mainImg.url=={{mainImg.url}


我认为您在直接绑定到原语时遇到了这个问题:

重点矿山

作用域继承通常是直接的,您甚至不需要知道它正在发生。。。直到您尝试从子作用域内部将双向数据绑定(即表单元素、ng模型)到父作用域上定义的基元(例如数字、字符串、布尔值)为止。它不像大多数人期望的那样工作。发生的情况是,子作用域获取自己的属性,该属性隐藏/隐藏同名的父属性。这不是AngularJS正在做的事情——这是JavaScript原型继承的工作方式。新的AngularJS开发人员通常没有意识到ng repeat、ng switch、ng view、ng include和ng if都会创建新的子作用域,因此当涉及到这些指令时,问题往往会出现。(有关问题的快速说明,请参阅。)

通过遵循“最佳实践”,可以很容易地避免原语的这个问题–观看3分钟。Misko演示了ng开关的基本绑定问题

直接显示您的问题(来源如下):

javascript:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  
  /*
  ng-repeat generates new scopes which will be child scopes of the scope within
  which they are generated. In other words, this scope is the parent scope for
  the child scopes generated by the ng-repeat in this example. Child scopes
  inherit things from their parent's scope.
  */

  // The initial main image 
  var initialImg = "http://3.bp.blogspot.com/-z8kzafZYkfQ/UERf6IbjJJI/AAAAAAAAALE/qaAxqqawXpM/s1600/Cat+Pictures+1.jpg";
  
  /*
  A primitive holding the URL for the main image
  
  This scope's child scopes will "shadow" this primitive, which basically means
  they'll get their own copy that is initialy the same value. The child scopes
  can only see their own copy though, so modifying the value in the child scope
  does not affect the value in the parent scope.
  */
  $scope.mainImgUrl = initialImg;
  
  /*
  An object holding the URL for the main image
  
  This scope's child scopes will NOT get their own copy of this object.
  Referencing main or main.imgUrl in the child scope will reference this object
  on this scope (unless the child scope explicitly define its own "mainImg" object.)
  */
  $scope.mainImg = { url: initialImg };
  
  // Our 'thumbnail' images
  $scope.images = [
      "http://happy.fm/wp-content/uploads/2011/10/random-owl.jpg",
      "http://www.superhumor.com/emoticonos/8761.gif"
  ];
  
});
html:


安古拉斯普朗克
文件。写(“”);
ng在父范围上的ng重复设置值内单击

举例说明原型继承的细微差别。看见
和
.

使用原语: 这是主映像的父范围

$scope.mainImgUrl=={{mainImgUrl}

使用ng repeat生成的拇指,使用ng click setting$scope.mainImgUrl(单击拇指查看发生了什么):

这是ng repeat生成的子作用域

$scope.mainImgUrl=={{mainImgUrl}

使用对象: 这是主映像的父范围

$scope.mainImg.url=={{mainImg.url}

使用ng repeat生成的拇指,使用ng click设置$scope.mainImg.url(单击拇指查看发生了什么):

这是ng repeat生成的子作用域

$scope.mainImg.url=={{mainImg.url}


Hi我已经改变了您描述的方式,但是仍然存在相同的问题(更新的JSFIDLE-)Hi我已经改变了您描述的方式,但是仍然存在相同的问题(更新的JSFIDLE-)