$ scope.feeds得到更新,但认为不会更新视图

问题描述 投票:0回答:1

$ scope.feeds是越来越控制器内更新。但视图不会更新变量。饲料变量是在NG-重复。

角代码:

$scope.openfeeds = function(category){
    $http({
        method: 'GET',
        url:  '/category/feeds/'+category.id
    }).then(function successCallback(response) {
         $scope.feeds = response.data;
         console.log($scope.feeds);
    });
};

HTML代码:

<div class="c-Subscribe">
        <div class="c-Subscribe_feeds" ng-repeat="feed in feeds" ng-controller="LinkController">
            @{{feed}}
        </div>
    </div>

然而,还有另一种称为类其右上变量。它是越来越有我这样做更新的饲料以同样的方式更新。

<div class="c-modal_content">
        <div class="c-categoryTile_blocks">
            <div class="c-categoryTile" ng-repeat="category in categories">
                <div class="c-categoryTile_background" style="background-image: url('/images/biker-circuit-competition-63249.jpg');">
                    <a href="" ng-controller="LinkController" ng-click="openfeeds(category)">@{{ category.category }}</a>
                </div>
            </div>
        </div>
    </div>

任何想法,为什么这是不工作?

javascript angularjs web
1个回答
0
投票

差不多是我给这个问题How to preserve scope data when changing states with ui-router?相同的答案

您需要下架的继承原型是如何工作的。当父母把与范围的属性值

$scope.value = 'something';

在子组件,如果你访问$ scope.value继承链会发现$ scope.value。

如果孩子套

$scope.otherValue = 'something';

如果遵循继承链,没有找到otherValue的值,并创建在子范围属性,而不是继承的原型,以父组件和家长的任何其他孩子看不出来。

您可以使用所谓原型继承的点规则。如果父上创建的范围对象调用像数据

$scope.data = { value: 'something' };

现在,如果孩子把一个属性的数据对象

$scope.data.otherValue = 'something';

它会查找数据对象,发现它在继承链,因为你将属性添加到对象的实例,这是家长和家长的任何子女可见。

let parent = {
  value: 'some value',
  data: { value: 'some value' }
};

let child = Object.create(parent);

console.log(child.value); // Finds value on the prototype chain

child.newValue = 'new value'; // Does not affect the parent

console.log(parent.newValue);

child.data.newValue = 'new value'; // newValue is visible to the parent

console.log(parent.data.newValue);

简短的回答是从来没有注入$范围和使用controllerAs语法。

要共享控制器之间的数据使用将注入到两个控制器的服务。您对服务点的收集和使用路由参数去找出其他的控制器应使用或对所谓currentSpot由其他控制器设置的服务的地方进行现场。

服务是可以在模块级创建,然后在自己的依赖列表要求他们所有控制器得到相同的实例的单个对象。他们共享控制器之间的数据的首选方式,$范围层次结构必然导致混乱,因为他们的原型继承性质可能会造成混淆。一个孩子$范围prototypally继承了它的父,这似乎是你应该共享数据,但是当孩子控制器设置属性是不父可见。

你学的角度编程是一种过时的。注射$范围已不再是推荐的方式。看看使用的组件。部件是用于与分离的范围,并使用contollerAs语法的控制器的包装。隔离范围使它更清洁知道在哪里的数据从何而来。

看看我的回答对这个问题

Trying to activate a checkbox from a controller that lives in another controller

© www.soinside.com 2019 - 2024. All rights reserved.