为什么angularjs单向绑定函数运行两次?

时间:2017-02-17 08:07:08

标签: javascript angularjs ionic-framework

我有一个模态视图:

<ion-content id="friendContent" class="scroll-content has-header has-subheader" style="background-color: rgba(0, 0, 0, 0.6);">
    <ion-list class="list">
        <ion-item ng-repeat="objFriendInfo in objFriendInfoList | filter:{ Remark: data.searchQuery }" class="item item-avatar-left item-button-right searchFriendItemBackgroundColor"
            ng-click="FriendList('chat',objFriendInfo,$event)">
            <img class="imgFriendUser" ng-class="{ 'gray':objFriendInfo.state == 0 }" ng-src="{{::objFriendInfo.icon }}">
            <h2 class="light">{{::GetAndSaveFriendName('friendlist_UserName',objFriendInfo.Remark,'16px', objFriendInfo) }}</h2>
            <h3 class="positive" ng-click="FriendList('travellog',objFriendInfo,$event)">{{::GetAndSaveFriendTravellogName('friendlist_TravellogName',objFriendInfo.TravelLog_Name,'14px', objFriendInfo) }}</h3>
            <div class="buttons" style="top:15px !important;">
                <button class="center button button-small button-positive ion-edit" ng-if="objFriendInfo.id != user.id" style="font-size:16px;" ng-click="FriendList('remark',objFriendInfo,$event)"></button>
                <button class="center button button-small button-positive icon-mapsay_delete" ng-if="objFriendInfo.id != user.id" style="font-size:25px;" ng-click="FriendList('delete',objFriendInfo,$event)"></button>
            </div>
        </ion-item>
    </ion-list>
</ion-content>

,函数GetAndSaveFriendName代码为:

$scope.GetAndSaveFriendName = function (name, text, fontSize, objFriendInfor) {
                var strDisplayName = "";

                console.log(text);

                if (objFriendInfor.strDisplayName) {
                    strDisplayName = objFriendInfor.strDisplayName;    
                } else {
                    strDisplayName = ServiceForChatCommon.GetShowDivText(name, text, fontSize);
                    objFriendInfor.strDisplayName = strDisplayName;
                }

                return strDisplayName;
            };

问题是GetAndSaveFriendName函数会运行两次,原因是什么?

[编辑]

我对控制器进行了十分判断:

.config([
        '$stateProvider',
        function ($stateProvider) {
            $stateProvider
                .state('tabs.chat', {
                    url: '/chat',
                    views: {
                        'chatView': {
                            templateUrl: 'components/chat-component/chat-view.html',
                            controller: 'chatController'
                        }
                    }
                })
        }]);

1 个答案:

答案 0 :(得分:0)

我想我找到了原因。 AngularJs的$ digest运行至少两次,第二次recyle确保DOM是最新的。