简体   繁体   English

Ng-src在AngularJS视图中不更新

[英]Ng-src doesn't update in AngularJS view

I'm using the following code in my Angular app to display an image: 我在Angular应用程序中使用以下代码来显示图像:

<img ng-src="{{planet.image_url}}" class="planet-img"/>

And I'm using $watch to change the image_url attribute when other events happen. 我正在使用$watch在其他事件发生时更改image_url属性。 For example: 例如:

$scope.$watch('planet', function(planet){
  if (planet.name == 'pluto') {
     planet.image_url = 'images/pluto.png';
  }
});

Using console logs, I see that the model attributes are changing just like I want them to, but these changes are not reflected in the DOM. 使用控制台日志,我看到模型属性正在改变,就像我想要的那样,但这些更改不会反映在DOM中。 Why isn't ng-src updating automatically as the model changes? 为什么ng-src在模型更改时不会自动更新? I'm new to Angular, so maybe this is a concept I haven't yet grasped. 我是Angular的新手,所以也许这是一个我尚未掌握的概念。 Any help will be greatly appreciated. 任何帮助将不胜感激。

You are using $scope.$watch in a wrong way. 你正在使用$ scope。$以错误的方式观看。 Please see the documentation: 请参阅文档:

function(newValue, oldValue, scope): 
called with current and previous values as parameters.

So the function is passed the old and the new value and the scope. 因此该函数传递旧的和新的值和范围。 So if you want to make updates to your data, you will need to reference the scope. 因此,如果要更新数据,则需要引用范围。 As this will equal to $scope here anyway, you can just use $scope directly and don't care for any parameter. 因为这将等于$ scope,你可以直接使用$ scope而不关心任何参数。 Do this: 做这个:

$scope.$watch('planet', function(){
  if ($scope.planet.name == 'pluto') {
    $scope.planet.image_url = 'images/pluto.png';
  }
});

Or if you want to use the scope passed to the function (as said, it will not make a difference at least here): 或者如果你想使用传递给函数的作用域(如上所述,它至少在这里不会产生任何影响):

$scope.$watch('planet', function(newval, oldval, scope){
  if (newval.name == 'pluto') {
    scope.planet.image_url = 'images/pluto.png';
  }
});

The best I can tell by this working CodePen example that I created everything should work just fine. 我可以通过这个有用的CodePen示例告诉我,我创建的所有东西应该可以正常工作。 Take a look at what I did and let me know if I am missing something. 看看我做了什么,让我知道如果我错过了什么。

I hope this helps. 我希望这有帮助。

The template: 模板:

<section class="well" ng-app="app" ng-controller="MainCtrl">
  Select Planet:<br>
  <label>Earth <input type="radio" ng-model="planetId" value="1" /></label>
  <label>Mars <input type="radio" ng-model="planetId" value="2" /></label>

  <img ng-src="{{currentPlanet.url}}" />
  <span class="label">{{currentPlanet.label}}</span>
</section>

The code: 代码:

var app = angular.module('app', []);

app.controller('MainCtrl', function($scope) {
  $scope.currentPlanet = {};

  $scope.planets = [{
    id: 1,
    label: 'Earth',
    url: 'http://s10.postimg.org/uyggrc14l/earth.png'
  },{
    id: 2,
    label: 'Mars',
    url: 'http://s21.postimg.org/maarztjoz/mars.png'
  }];

  $scope.$watch('planetId', function(id) {
    for(var i = 0; i < $scope.planets.length; i++) {
      var planet = $scope.planets[i];
      if(planet.id == id) {
        $scope.currentPlanet = planet;
        break;
      }
    }
  });
});

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

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