简体   繁体   English

AngularJS:在ng-repeat中更新范围

[英]AngularJS : Updating scope in ng-repeat

I have an Angular app with several nested controllers and views. 我有一个带有几个嵌套控制器和视图的Angular应用程序。 I implemented infinite scrolling in it closely based on this tutorial for ngInfiniteScrolling: http://binarymuse.github.io/ngInfiniteScroll/demo_async.html 我根据ngInfiniteScrolling的本教程在其中紧密实现了无限滚动: http ://binarymuse.github.io/ngInfiniteScroll/demo_async.html

So I have a service that loads items into an array at $scope.content.items . 所以我有一个将项目加载到数组中的服务,位于$scope.content.items Then there's an ng-repeat element that shows each result. 然后是一个ng-repeat元素,显示每个结果。

$scope.content = new Content()
$scope.content.loadMore( $scope.currentStream, 2 ) // this part is actually called in the HTML, but while debugging I've just done it in the controller

Now I want to implement search, and instead of making another search page, just have the items load in place of the current list of items. 现在,我想实现搜索,而不是创建另一个搜索页面,只需加载项目即可代替当前的项目列表。 Basically to take the place of $scope.content.items . 基本上可以代替$scope.content.items

So I built an identical controller, but now calling my search API. 因此,我建立了一个相同的控制器,但现在调用了我的搜索API。 I use ng-change to see if someone has typed in the search box, then within the function that calls, do 我使用ng-change来查看是否有人在搜索框中输入内容,然后在调用的函数中执行

$scope.search = function() {
    $scope.content = new Search()
    $scope.content.load( $scope.query )
}

I can see that this works in the console, that it replaces $scope.content.items , by doing this in the browser console: 我可以看到这在控制台中$scope.content.items ,通过在浏览器控制台中执行以下操作,它替换了$scope.content.items

var scope = angular.element($('[ng-controller=HomeController]')).scope()
scope.content.items

That shows me the array of objects I expect in each case (either before triggering ng-change="search()" or after). 这向我展示了在每种情况下(触发ng-change="search()"或之后)期望的对象数组。 But the page itself does not update. 但是页面本身不会更新。 It just shows the stuff from the Content() service. 它只是显示Content()服务中的Content()

Likewise, if I replace the above two lines from my controller with these below, it shows the content from the Search() service: 同样,如果我将控制器中的上述两行替换为以下两行,则它显示Search()服务的内容:

$scope.content = new Search()
$scope.content.load( 'thom' )

Long story short , I feel like the services and API work, but the page is not updating when I change the $scope.content.items array used by ng-repeat . 长话短说 ,我感觉服务和API都可以工作,但是当我更改ng-repeat所使用的$scope.content.items数组时,页面没有更新。


Here is the HTML 这是HTML

<div class="panel panel-item" ng-repeat="item in content.items" ng-hide="hideItem">
    <h2 ng-hide=" item.stream == 'read' " data-ng-bind="item.title"></h2>

    <a ng-click="openReaderModal( item )" class="cursor-pointer" ng-show=" item.stream == 'read' ">
        <h2 data-ng-bind="item.title"></h2>
    </a>

    // ...

    <div class="clearfix"></div>
</div>

Fixed it, somehow. 修复它,以某种方式。 Here is my routes from app.config() before : 这是我从app.config()出发之前的路线:

$stateProvider
    // ...
    .state( 'app', {
        url: '/app',
        templateUrl: 'app/views/app.html',
        controller: 'HomeController'
    })
    .state( 'app.home', {
        url: '/main',
        templateUrl: 'app/views/home.html',
        controller: 'HomeController'
    })
    .state( 'app.profile', {
        url: '/profile',
        templateUrl: 'app/views/profile.html',
        controller: 'ProfileController'
    })
    .state( 'app.read', {
        url: '/read',
        templateUrl: 'app/views/stream-content.html',
        controller: 'HomeController'
    })
    .state( 'app.watch', {
        url: '/watch',
        templateUrl: 'app/views/stream-content.html',
        controller: 'HomeController'
    })
    .state( 'app.listen', {
        url: '/listen',
        templateUrl: 'app/views/stream-content.html',
        controller: 'HomeController'
    })

And here's after : 之后

$stateProvider
    // ...
    .state( 'app', {
        url: '/app',
        templateUrl: 'app/views/app.html',
        controller: 'HomeController'
    })
    .state( 'app.home', {
        url: '/main',
        templateUrl: 'app/views/home.html'
    })
    .state( 'app.profile', {
        url: '/profile',
        templateUrl: 'app/views/profile.html',
        controller: 'ProfileController'
    })
    .state( 'app.read', {
        url: '/read',
        templateUrl: 'app/views/stream-content.html'
    })
    .state( 'app.watch', {
        url: '/watch',
        templateUrl: 'app/views/stream-content.html'
    })
    .state( 'app.listen', {
        url: '/listen',
        templateUrl: 'app/views/stream-content.html'
    })

And it works. 而且有效。 If anyone can provide an explanation, I'll credit them the answer. 如果有人可以提供解释,我会把答案归功于他们。

The routing in angular works like this. 角形铣削的工作方式是这样的。

When we provide a controller for the $stateProvider its actually considered as a new constructor (new keyword as in java) thus the data is re-initiated to defaults. 当我们为$ stateProvider提供一个控制器时,它实际上被视为一个新的构造函数(如java中的new关键字),因此数据将重新初始化为默认值。 The new constructor will be the child to itself, to access the parent controller one can use the $parent 新的构造函数将是其自身的子代,要访问父控制器,可以使用$ parent

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

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