angular.js - Value update issue in view in angularjs
PHP中文网
PHP中文网 2017-05-16 13:18:53
0
1
619

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') }) } } })

Homepage


Balance


Calculate


< p tab-img class="I_AF_p" ng-click="goto('ck')">View


Documentation

Get the index after clicking the corresponding p, and then change the corresponding value in $scope.imgsrc. The console can correctly output the image address, but why does the value in ng-src in the view need to be clicked twice to change? I just want to Change the image address in ng-src. I just started learning angularjs. Can someone explain it to me? Thank you.

PHP中文网
PHP中文网

认证高级PHP讲师

reply all (1)
洪涛

You need to manually trigger angular’s rendering mechanism

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') }) }) }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!