功能中的AngularJS范围似乎不起作用

时间:2015-04-18 22:15:55

标签: javascript angularjs service controller factory

我有两个ajax调用。数据从html(输入)开始,当按下enter时,输入字段中的内容被发送到控制器,然后发送到进行第一次ajax调用的工厂。成功在控制器中处理,然后请求另一个ajax调用,并且再次在控制器中处理来自该请求的数据。我有一个$scope - $scope.ytChannel = data.items;,但在功能中最终没有成功。这是我的代码以html

开头

HTML:

<input class="form-control channel-index" type="text" ng-model="channel" placeholder="Enter Channel" ng-keydown="$event.which === 13 && cname(channel)"/>

JS:

.factory('ytVids', function($http){

    return{
        getChannel: function(name){
            var url = 'https://www.googleapis.com/youtube/v3/channels? part=contentDetails&forUsername='+ name +'&key=[my api key]';
            return $http.get(url);
        },
        getVids: function(channel){
            return    $http.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId='+channel+'&key=[my api key]');

        }
    };
  })
  .controller('MainCtrl', function ($scope, ytVids) {
     $scope.cname = function(channel){
        ytVids.getChannel(channel).success(function(response){
            //console.log(response);
            console.log(response.items[0].contentDetails.relatedPlaylists.uploads);
            ytVids.getVids(response.items[0].contentDetails.relatedPlaylists.uploads)
            .success(function(data){
                console.log('in success');
                console.log(data.items);
                $scope.ytChannel = data.items; // This is the scope that is not seeming to want to work. 
            });
        });
    };
  });

这是调用ytChannel

的html
<ul>
  <li ng-repeat="item in ytChannel" class="col-xs-12 col-sm-6 col-md-4 vid-options">
    <a href="#">
      <div class="title">{{item.snippet.title}}</div>
      <img src="{{item.snippet.thumbnails.maxres.url}}" />
    </a>
  </li>
</ul>

如果范围在函数中,html是否无法访问它?可以做什么,以便我可以访问返回的数据?

错误

这是控制台在开发工具GET http://localhost:9000/%7B%7Bitem.snippet.thumbnails.maxres.url%7D%7D 404 (Not Found)

中提供的错误

1 个答案:

答案 0 :(得分:2)

正确的代码是

<img ng-src="{{item.snippet.thumbnails.maxres.url}}" />

作为手册says

  

在src属性中使用像{{hash}}这样的Angular标记并不起作用   右:浏览器将使用文字文本从URL中获取   {{hash}}直到Angular替换{{hash}}中的表达式。该   ngSrc指令解决了这个问题。

相关问题