在组件模块angular JS中使用$ http

时间:2016-06-03 08:52:13

标签: angularjs angularjs-scope angularjs-controller angularjs-components

我想了解为什么我无法从外部来源检索数据。 我需要恢复Json的内容并解析它以显示此模板中的内容:

新闻报道:{{new.length}}

<div>
    <lignevin class="ligne-vin" ng-repeat="new in $ctrl.news">
        <etiquevin class="etiquettevin">
            <img class="etiquette" src="{{new.img}}"/>
        </etiquevin>
        <descriptifvin class="descriptif-vin">
            <p class="title">{{new.name}}</p>
            <p class="sub-title">{{new.ref}}</p>
            <p>{{new.prixTotal}}</p>
            <p>{{new.prixUnit}}</p>
        </descriptifvin>
    </lignevin>
</div>

当我使用此代码时,它无法正常工作且没有任何消息:

function NewsListCtrl($scope, $http) {
    $http.get('http://myndd/myfile.json').success(function (data) {
        $scope.news = data;
        console.log($scope);
    });
};

angular.
    module('myApp').
    component('newsList',{
        templateUrl :'templates/newsdetail.html',
        controller: NewsListCtrl,
        bindings: {
            news: '='
        }
    }
);

但是如果不像这样从外部调用数据:它可以正常工作

angular.
    module('myApp').
    component('newsList',{
        templateUrl :'templates/newsdetail.html',
        controller: function NewsListCtrl(){
            this.news=[
                {
                    "id": 1,
                    "name": "name1",
                    "ref": "ref1",
                    "conditionnement" : "cond1",
                    "prixTotal": "10,00€",
                    "prixUnit" : "1,00€",
                    "img": "http://myndd/myfile1.png"
                },
                {
                    "id": 2,
                    "name": "name2",
                    "ref": ref2",
                    "conditionnement" : "cond2",
                    "prixTotal": "20,00",
                    "prixUnit" : "2,00€",
                    "img":"http://myndd/myfile2.png"
                }
            ];
        }
    }
);

1 个答案:

答案 0 :(得分:1)

对不起,我在发布此内容后立即尝试了最后一件事,找到了答案。 我的mystake在模板的循环中:

<lignevin class="ligne-vin" ng-repeat="new in news">