简体   繁体   English

为什么更改视图时将Angularjs物料卡推到内容下方?

[英]Why are the Angularjs Material Cards being pushed below the content on a view change?

I have a page that when visited after a view change, the content is being added to the bottom of the page instead of the view being repopulated with the new content. 我有一个页面,在视图更改后访问该页面时,该内容将被添加到页面底部,而不是用新内容重新填充该视图。 This is only occurring on this on page with the AngularJS Material Design Directives specifically the cards. 这仅在AngularJS Material Design Directives专门用于卡片的页面上发生。 All other directives work without hesitation. 所有其他指令都可以毫不犹豫地工作。 If the directives are removed from this page then the view will change state without the content being added to the bottom of the page as it should. 如果从此页面中删除了指令,则视图将更改状态,而内容不会按原样添加到页面底部。

I also get the following error on my directory page: error: [$rootScope:infdig] 10 $digest() iterations reached. 我的目录页面上也出现以下错误:错误:[$ rootScope:infdig]达到10个$ digest()迭代。 Aborting! 流产! Is there another way I need to write this service to prevent this from happening? 我是否需要编写另一种方法来防止这种情况发生? Is this the reason for my directory page being pushed to the bottom of another page? 这是我的目录页面被推到另一页面底部的原因吗?

An example of what this looks like can be found here: https://www.dropbox.com/s/tiohab2dm4gzhcx/angular-issue.png?dl=0 可以在以下位置找到此示例的示例: https : //www.dropbox.com/s/tiohab2dm4gzhcx/angular-issue.png?dl=0

=== DIRECTORY CONTROLLER === ===目录控制器===

angular.module('main').controller('directory', ['$scope', 'rows',    function($scope, rows){
$scope.imagePath = 'http://lorempixel.com/250/250/business/'; 
$scope.people = [
    {
        name: 'asdfasdf',
        dept: 'asdf',
        desc: 'asdfasdf',
        phone: '33333333',
        email: 'adsfasdf@asdf.com',
        photo: 'something.jpg'
    }
$scope.chunked = function(){
    return rows($scope.people, 3); 
}

=== ROWS SERVICE === ===行服务===

angular.module('main').factory('rows', function()
{
    return function(objects, num)
    {
        var newArr = [];
       for (var i=0; i<objects.length; i+=num) {
        newArr.push(objects.slice(i, i+num));
       }
       return newArr;
    }
});

=== HTML === === HTML ===

<div class="container-fluid">
  <div class="container" ng-controller="directory" id="directory">
  <h1>Directory</h1>
  <div class="row" ng-repeat="people in chunked()">
  <div class="col-sm-4" ng-repeat="person in people">
  <md-card md-theme="{{ showDarkTheme ? 'dark-purple' : 'default' }}" md-theme-watch="">
  <md-card-title>
  <md-card-title-text>
    <span class="md-headline">{{person.name}}</span>
    <span class="md-subhead">{{person.dept}}</span>
    <span class="md-subhead">{{person.desc}}</span>
    </md-card-title-text>
    </md-card-title>
    <md-card-actions layout="row" layout-align="end center">
        <md-button class="md-fab md-mini md-primary" aria-label="Favorite" href="mailto: {{person.phone}}">
            <md-icon class="material-icons">phone</md-icon>
        </md-button>
        <md-button class="md-fab md-mini md-primary" aria-label="Settings" href="mailto: {{person.email}}">
            <md-icon class="material-icons">email</md-icon>
        </md-button>
        </md-card-actions>
        </md-card>
      </div>
    </div>
  </div>
</div>

=== ROUTES === ===路线===

angular.module('main').config(['$routeProvider',
function($routeProvider)
{
    $routeProvider.
    when('/:paramB',
    {
        templateUrl: function(params)
        {
            params.paramB = params.paramB.replace(':', '');
            return "/pages/" + params.paramB;

        }
    }).when('/', {
            templateUrl: function(){
                return '/home.asp'
            }
    })
}
]);

Try returning an object from your factory, rather than a function, like so: 尝试从工厂而不是函数中返回一个对象,如下所示:

angular.module('main').factory('rows', function()
{
    return {
        calculate: function(objects, num)
        {
            var newArr = [];
           for (var i=0; i<objects.length; i+=num) {
                newArr.push(objects.slice(i, i+num));
           }
           return newArr;
        }
    };
});

Then use it like this: return rows.calculate($scope.people, 3); 然后像这样使用它: return rows.calculate($scope.people, 3);

This was indeed an issue with ng-repeat. 这确实是ng-repeat的问题。 If there is a iterator warning then this can effect the view state. 如果存在迭代器警告,则这可能会影响视图状态。 The solution for this was to prevent this error by utilizing the following. 解决方案是通过利用以下内容来防止此错误。 This is the long way, outside of creating a factory/service, however this satisfies Angular. 除了创建工厂/服务之外,这是很长的路要走,但这满足了Angular。

<div class="row" ng-repeat="person in people" ng-if="$index % 3 == 0">
  <div class="col-sm-4">{{people[$index].name}}</div>
  <div class="col-sm-4">{{people[$index + 1].name}}</div>
  <div class="col-sm-4">{{people[$index + 2].name}}</div>
</div>

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

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