AngularJS:使用控制器和ng-repeat在div上重新加载数据 [英] AngularJS: Reload data on div with controller and ng-repeat

查看:39
本文介绍了AngularJS:使用控制器和ng-repeat在div上重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular JS的新手,正在学习它.我有一个div,并在启动时使用以下代码从json加载数据,但我想在执行特定操作后更改json对象时再次重新加载数据.

I am new in Angular JS and learning it. I have a div and load data from json on startup with controller with following code but I want to reload it again when json object changed after performing specific action.

index.html

index.html

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ng-app="ezpf" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="lib/angular.js"></script>
        <script src="lib/jquery.min.js"></script>
    </head>
    <body onload="EZPF.Application.initialize()">
        <div id="btn-import" class="my-button button-small" onClick="EZPF.Application.openFile(this)">
                    <span class="button-title">Import</span>
                    <span class="button-help">This button will do something                         else.</span>                        
        </div>
        <div class="page" id="pro" ng-controller="ProductsListCtrl as store">
             <div ng-repeat="product in store.products.Products">                     
                  {{product.Title}}
             </div>
        </div>
    </body>
</html>

myAngular.js

myAngular.js

(function()
{
    var app = angular.module('ezpf', []);
    app.controller('ProductsListCtrl',['$scope', function($scope)
    {
        this.products = EZPF.Application.getProducts();     
        $scope.reload = function()        
        {           
            $scope.products = EZPF.Application.getProducts();           
        };
    }]);
})();

在下面的javascript文件中,我正在打开JSON文件,并使用新数据重新加载产品对象.用新的JSON文件内容更新后,我必须重新加载数据.我试图从控制器调用重载,但无法正常工作.请帮助我,谢谢.

In following javascript file I am opening JSON file and reload products object with new data. After updating with new JSON file contents I have to reload data. I have tried to call reload from controller but its not working. Please help me, thanks in advance.

application.js

application.js

var EZPF;
if (!EZPF) 
    EZPF = {};
if (!EZPF.Application) 
    EZPF.Application = {};


EZPF.Application = 
{    
    products: [],
    getProducts: function()
    {
        if (this.products.length == 0) 
        {
            this.products = 
            {
                "Products": [
                {
                     "Title": "default name"
                     ....
                }]
            }
        }
        return this.products;
    },
    openFile: function()
    {
        var docsDir = air.File.documentsDirectory;
        try 
        {
            var jsonFilter = new air.FileFilter("JSON Files", "*.json");
            docsDir.browseForOpenMultiple("Select JSON Files", [jsonFilter]);
            docsDir.addEventListener(air.FileListEvent.SELECT_MULTIPLE, filesSelected);
        } 
        catch (error) 
        {
            air.trace("Failed:", error.message)
        }

        function filesSelected(event)
        {
            air.trace(event.files[0].nativePath);
            var myFile = new window.runtime.flash.filesystem.File();
            var file = myFile.resolvePath(event.files[0].nativePath);
            var fileStream = new air.FileStream();
            fileStream.open(file, air.FileMode.READ);
            this.products = fileStream.readMultiByte(fileStream.bytesAvailable, air.File.systemCharset);
            fileStream.close();
            air.trace(products);
            $('#pro').reload();
        }
    }
};

推荐答案

您正在使用controller as(ng-controller="ProductsListCtrl as store")语法,因此必须将变量分配给控制器本身(this),而不是$scope:

You are using the controller as (ng-controller="ProductsListCtrl as store") syntax, so you have to assign the variables to the controller itself (this) instead of the $scope:

var vm = this;

vm.products = EZPF.Application.getProducts();     
vm.reload = function()        
{           
    vm.products = EZPF.Application.getProducts();           
};

要重新加载数据:

<div class="page" id="pro" ng-controller="ProductsListCtrl as store">
    <div ng-repeat="product in store.products.Products">                     
        {{product.Title}}
    </div>
    <!-- Button for reloading the data -->
    <button ng-click="store.reload()">Reload Data</button>
</div>

JSFIDDLE

这篇关于AngularJS:使用控制器和ng-repeat在div上重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆