angular.js - angularjs中视图中的值更新问题
PHP中文网
PHP中文网 2017-05-16 13:18:53
0
1
639

SolarTerms.controller('FooterCtrl', ['$state', '$rootScope', '$scope', function ($state, $rootScope, $scope) {

$scope.imgsrc = {
    "imda1":"img/icon1.png",
    "imda2":"img/icon2.png",
    "imda3":"img/icon3.png",
    "imda4":"img/icon4.png",
    "imda5":"img/icon5.png"
}

}])

SolarTerms.directive("tabImg",function () {

    return {
        restrict:'A',
        scope:false,
        link:function (scope,ele,attr,ctrl) {
            ele.bind('click',function (event) {
                var index = $(this).index();
                scope.imgsrc = {
                    "imda1":"img/icon1.png",
                    "imda2":"img/icon2.png",
                    "imda3":"img/icon3.png",
                    "imda4":"img/icon4.png",
                    "imda5":"img/icon5.png"
                };
                scope.imgsrc['imda'+(index+1)] = 'img/icon'+(index+1)+'1.png';
                //scope.$apply(scope.imgsrc['imda'+(index+1)] = 'img/icon'+(index+1)+'1.png')
            })
        }
    }
})

<p tab-img class="I_AF_p" ng-click="goto('index')"><img ng-src="{{imgsrc.imda1}}" alt=""><span>首页</span></p>
<p tab-img class="I_AF_p" ng-click="goto('ye')"><img ng-src="{{imgsrc.imda2}}" alt=""><span>余额</span></p>
<p tab-img class="I_AF_p" ng-click="goto('js')"><img ng-src="{{imgsrc.imda3}}" alt=""><span>计算</span></p>
<p tab-img class="I_AF_p" ng-click="goto('ck')"><img ng-src="{{imgsrc.imda4}}" alt=""><span>查看</span></p>
<p tab-img class="I_AF_p" ng-click="goto('wd')"><img ng-src="{{imgsrc.imda5}}" alt=""><span>文档</span></p>

点击相应的p之后获取索引,然后更改$scope.imgsrc中相应的值,控制台能正确输出图片地址,但是视图中ng-src中的值为什么需要点击两次才能改变,我只是想更改ng-src中的图片地址,刚开始学angularjs,谁帮我解释下,谢谢。

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
洪涛

需要手动触发angular的渲染机制

 link:function (scope,ele,attr,ctrl, $timeout) {
            ele.bind('click',function (event) {
                var index = $(this).index();
                scope.imgsrc = {
                    "imda1":"img/icon1.png",
                    "imda2":"img/icon2.png",
                    "imda3":"img/icon3.png",
                    "imda4":"img/icon4.png",
                    "imda5":"img/icon5.png"
                };
                
                $timeout(function() {
                    scope.imgsrc['imda'+(index+1)] = 'img/icon'+(index+1)+'1.png';
                    //scope.$apply(scope.imgsrc['imda'+(index+1)] = 'img/icon'+(index+1)+'1.png')
                })
                
            })
        }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!