简体   繁体   English

AngularJS中的嵌套部分和对象引用

[英]Nested Partials and Object References in AngularJS

I have a partial view that looks like: 我有一个局部视图,看起来像:

<div ng-if="activeItem.typeOf == 'text'">
     <div onload="item=activeItem" ng-include="'views/app-builder/text.html'"></div>
</div>

when the users clicks a button I have a controller method that resets the activeItem 当用户单击按钮时,我有一个控制器方法可以重置activeItem

 $scope.showDetails = function(item){
        $scope.activeItem = item;
 };

with activeItem looking like: 与activeItem看起来像:

{  name: "Candy", typeOf: "text" }

it works as expected the first time, but every time after that the active item in the nested partial is never updated. 它第一次按预期工作,但是每次之后嵌套部分中的活动项目就永远不会更新。 Likely because the reference onload was never updated. 可能是因为参考onload从未更新过。 What is the 'right' way to handle this in angular? 正确处理此问题的“正确”方法是什么?

Always use a reference to facilitate AngularJS to track the change via prototype. 始终使用引用来帮助AngularJS通过原型跟踪更改。 This is the nature of Javascript. 这就是Javascript的本质。 Try this approach: 试试这种方法:

$scope.activeItem = {};
$scope.activeItem.values = {
    name: "Candy0",
    typeOf: "text0"
}

$scope.showDetails = function (item) {
    $scope.activeItem.values = item;
}

In the view 在视图中

{{item.values.name}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM