简体   繁体   English

如何在Angular 1.5中使用单向绑定? 除了手表,还有其他选择吗?

[英]How to use One Way binding in Angular 1.5? Any alternative to watches?

Component.HTML file: Component.HTML文件:

<div>
  <table class="table table-bordered table-responsive">
  <thead>
<tr>
  <th>Company</th>
  <th>Stock Price</th>
  <th>Last Updated Time</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="list in model.myLists">
  <th>{{list.company}}</th>
  <td>{{list.stockPrice}}</td>
  <td>{{list.lastUpdateTime}}</td>
</tr>
</tbody>
</table>
</div>

This is component.js file: 这是component.js文件:

(function() {
"use strict";
var module = angular.module("stockdApp");

// Global variables
var stockList = [];

function getStocks (model) {
// api gets stock values every 2 seconds and saves it to stockList variable
stockList = newList;
model.myLists = stockList;
}
function controller($http) {
 var model = this;
 model.$onInit = function() {     
        getStocks(model);            
 } 

 model.$onChanges = function (changes) {
   console.log("channges",changes);        
 };
};

module.component("stocks", {
    templateUrl: "./stock.component.html",
    controllerAs: "model",
    controller: ["$http", controller],
    bindings: {
        myLists: "<"
    }
});
}());

I have a api call which gets new data every 2 seconds and I want to update my table whenever I get the new data. 我有一个api调用,它每2秒获取一次新数据,并且我想在获取新数据时更新表。 I am using Angular 1.5 and not sure how to update the table. 我正在使用Angular 1.5,不确定如何更新表。

When you do 当你做

stockList = newList;
model.myLists = stockList;

you are changing the reference of the initial array. 您正在更改初始数组的引用。 What you need to do is remove the items from myList and add new ones while keeping the reference. 您需要做的是从myList中删除项目并添加新项目,同时保留引用。

Something like this: 像这样:

(function() {
"use strict";
var module = angular.module("stockdApp");

function getStocks ($http, model) {
    // Modify to fit your case...
    $http.get(....).then(function(newList) {
        // Empty the array and add the new items while keeping the same refernece
        model.myLists.length = 0;
        newList.forEach(function(newElment) { model.myLists.push(newElment); });
    });
}
function controller($http) {
 var model = this;
 model.myLists = [];

 model.$onInit = function() {     
        getStocks($http, model);            
 } 

 model.$onChanges = function (changes) {
   console.log("channges",changes);        
 };
};

module.component("stocks", {
    templateUrl: "./stock.component.html",
    controllerAs: "model",
    controller: ["$http", controller],
    bindings: {
        myLists: "<"
    }
});
}());

Maybe you could use $scope.$apply() } 也许您可以使用$ scope。$ apply() }

function getStocks (model) {
  $scope.$apply(function(){
   stockList = newList;
   model.myLists = stockList;
 });
}

That way you tell the browser that the content of the model, got updated. 这样,您就可以告诉浏览器该模型的内容已更新。

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

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