简体   繁体   English

角度列表未在监视中更新

[英]Angular list not updating on watch

I have been using some angular controllers for get/post requests a few times and thus not too experienced with angular (or JS in any way, I'm the backend type not frontend), but got a problem I have been trying to solve past week for a private project, without success. 我已经多次使用一些角度控制器来进行获取/发布请求,因此对角度(或者以任何方式使用JS,我不是后端类型,而是前端)不太有经验,但是遇到了一个我试图解决过去的问题一个私人项目一周,但没有成功。 I tried any solution I could find here. 我尝试了在这里可以找到的任何解决方案。 Hopefully someone can help. 希望有人可以提供帮助。

I get 1x JSON object from backend containing 4x strings, and a list of strings. 我从包含4x字符串和字符串列表的后端获得1x JSON对象。

{
  'var1':'abc',
  'var2':'def',
  'var3':'ghi',
  'var4':'jkl',
  'images':['images/abcd.jpg', 'images/efgh.jpg']
}

The gui updates all 4 strings but not the list. gui更新所有4个字符串,但不更新列表。

Controller: 控制器:

app.controller('ObjectInfoController', function ($scope, $window, $http) {

        $scope.$watch('images', function(newValue, oldValue) {
            alert(newValue);
        });

        $http.post("http://localhost:8080/view_object/", postdata).then(
            function(data, status, headers, config) {
            $scope.var1 = data.data.var1;
            $scope.var2 = data.data.var2;
            $scope.var3 = data.data.var3;
            $scope.var4 = data.data.var4;
            $scope.images = data.data.images;
            $scope.$digest();
        },
        function(err) {
            // Do something
        }
    );
});

HTML: HTML:

<div class="row" ng-controller="ObjectInfoController">
    <div class="col-md-3">
        <!-- Images -->
        <div class="owl-carousel content-slider-with-controls-autoplay">
            <div class="overlay-container overlay-visible" ng-repeat="image in images">
                <img src="{{image}}" alt="">
                <div class="overlay-bottom hidden-xs">

                </div>
                <a href="{{image}}" class="popup-img overlay-link"><i class="icon-plus-1"></i></a>
            </div>
        </div>
        <!-- Images end -->
    </div>
    <div class="col-md-8 pv-30 TextBlock bottomFix">
        <h2 class="TextBlockTitle">{{var1}}</h2>
        <ul class="nav">
            <li class="icon-right-open-outline"><strong>{{var2}}</strong></li>
            <li class="icon-right-open-outline"><strong>{{var3}}</strong></li>
            <li class="icon-right-open-outline"><strong>{{var4}}</strong></li>
    </ul>
  </div>
</div>

I saw people using watches to fix this problem (although I do not get why the list needs it but not remaining data), but cannot get it to work. 我看到人们使用手表来解决此问题(尽管我不明白为什么列表需要它但不需要剩余数据),但无法使它正常工作。

See the alert() I do inside the watch? 看到我在手表里面做的alert()吗? With that alert I get the images in my ng-repeat and all looks good. 有了这个警报,我得到了我的ng-repeat中的图像,并且看起来都不错。 Removing the alert, and it doesn't work. 删除警报,它不起作用。 Does an alert do some sort of "update" to the view, and can I force it without alerting? 警报是否会对视图进行某种“更新”,是否可以不发出警报就强制执行?

I actually get 2 alerts popping up, first is empty and second prints my list, but that is normal I found out. 我实际上弹出了2个警报,第一个是空的,第二个打印了我的列表,但这是我发现的正常现象。 I tried setting $scope.images inside watch too, with no luck. 我也尝试在手表内部设置$ scope.images,但是没有运气。

I feel there is either something trivial I didn't understand, or I should not use watch but something else? 我觉得有一些我不了解的琐事,或者我不应该使用手表而是其他东西? Please enlighten me if you know what I am doing wrong. 如果您知道我在做什么错,请告诉我。 I feel clueless by now. 我现在很无知。

And thanks for your time. 并感谢您的宝贵时间。

Edit: just did a random test and typed {{images[0]}} after {{var1}} and the correct value was written out on the page.... so the problem seems to only exist in my ng-repeat. 编辑:只是进行了一次随机测试,并在{{var1}}之后键入了{{images [0]}},并且在页面上写出了正确的值....所以问题似乎只存在于我的ng-repeat中。

I don't see a reason why you would need the $watch. 我看不出您需要$ watch的原因。 You also shouldn't call $digest unless you have a specific reason to do so. 除非有特殊原因,否则也不应调用$ digest。

It's a good idea to use the controllerAs-syntax as it makes the scope exlicit. 使用controllerAs语法是一个好主意,因为它使作用域成为显式。 Change your controller declaration in the html to <div class="row" ng-controller="ObjectInfoController as vm"> , and refer to your variables through vm in the template, eg ng-repeat="image in vm.images" . 将html中的控制器声明更改为<div class="row" ng-controller="ObjectInfoController as vm"> ,并通过vm在模板中引用变量,例如ng-repeat="image in vm.images"

app.controller('ObjectInfoController', function ($http) {
    var vm = this;

    $http.post("http://localhost:8080/view_object/", postdata).then(
        function(data, status, headers, config) {
            vm.var1 = data.data.var1;
            vm.var2 = data.data.var2;
            vm.var3 = data.data.var3;
            vm.var4 = data.data.var4;
            vm.images = data.data.images;
        },
        function(err) {
            // Do something
        }
    );
});

JSFiddle JSFiddle

Also, you should use ng-src instead of src when you use variables in the img-element source. 另外,在img-element源中使用变量时, 应使用ng-src而不是src

If it still doesn't work, I suspect the problem has something to do with owl-carousel - try to loop over the images variables outside the owl-carousel. 如果仍然不起作用,我怀疑问题与猫头鹰传送带有关-尝试在猫头鹰传送带之外遍历图像变量。

If it is possible two values in the images array are equal, change your ng-repeat expression to 如果images数组中的两个值相等,则将ng-repeat表达式更改为

ng-repeat="image in images track by $index"

ng-repeat with primitives requires some care. 使用原语的ng-repeat需要谨慎。 I'd have just suggested this in a comment but I don't have the rep. 我只是在评论中提出了建议,但没有代表。

See this and this 看到这个这个

Its because you are using primitive datatype and not object which provide two-way data binding. 这是因为您使用的是原始数据类型,而不是提供双向数据绑定的对象。

Primitive datatype is not provide two-way data binding. 原始数据类型不提供双向数据绑定。

vm.var1, vm.var2 etc are primitive datatype. vm.var1, vm.var2等是原始数据类型。

You should changes the script and html as follows. 您应按以下步骤更改脚本和html。

JS: JS:

$http.post("http://localhost:8080/view_object/", postdata).then(
            function(data, status, headers, config) {
            $scope.objData = data.data;
        },
        function(err) {
            // Do something
        }

HTML : HTML:

<ul class="nav">
    <li class="icon-right-open-outline"><strong>{{objData.var2}}</strong></li>
    <li class="icon-right-open-outline"><strong>{{objData.var3}}</strong></li>
    <li class="icon-right-open-outline"><strong>{{objData.var4}}</strong></li>
</ul>

Actually, all answers worked. 实际上,所有答案都有效。 Even my original code worked (which was a modified copy/paste from another page I used ng-repeat on. I found out by performing same ng-repeat code in a new div before the one that didn't work, just printing the {{image}} and had no problems. 甚至我原来的代码都可以正常工作(这是我使用ng-repeat的另一页的修改后的复制/粘贴。我发现,在无效的代码之前,在新的div中执行相同的ng-repeat代码,只需打印{ {image}},没有问题。

Problem I took for granted past few days were located in my controller, was actually a css problem... 我过去几天理所当然的问题位于我的控制器中,实际上是一个CSS问题...

Im sorry for wasting time, but thankful to learn a bit more about what the different code you posted is for :) 很抱歉浪费时间,但是非常感谢您能进一步了解您发布的不同代码的用途:)

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

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