简体   繁体   English

创建列表并使用angular指令进行渲染

[英]Creating a list and rendering with angular directives

I am doing a learning project for the MEAN stack, and I am really stuck at something which I need help. 我正在为MEAN堆栈做一个学习项目,而我确实停留在需要帮助的地方。

On the real project, what I do is I have a Form creator were different components can be created and arranged according to the user needs, then directives similar to the used on this simplified example renders the from component by component and enable the user to populate it. 在实际项目中,我要做的是创建一个Form创建器,可以根据用户需求创建和排列不同的组件,然后类似于此简化示例中使用的指令按组件呈现from组件,并允许用户填充它。

On this JSFiddle , as I mentioned, there is a simplified version where I use a similar approach to the one I want to use on my project. 正如我提到的那样 ,在这个JSFiddle上 ,有一个简化的版本,在该版本中,我使用与我要在项目中使用的方法类似的方法。

My Logic is: I create a new array where all the values I input on the textbox are stored after a small processing on the format, then I have two directives that have access to a factory function where the data is stored, that loop trough all the items and render them one by one. 我的逻辑是:我创建了一个新数组,对文本框输入的所有值在对格式进行少量处理后都存储在其中,然后我有两个指令可以访问存储数据的工厂函数,该指令遍历所有并逐一渲染它们。

I can see that the factory function is working and create the set of data as I wanted to be. 我可以看到工厂功能正在运行,并按我的意愿创建了数据集。

Here is where the problems start: 问题从这里开始:

I call the directive like this 我这样称呼指令

<render-all-items></render-all-items>

the definition of this directive is 该指令的定义是

.directive('renderAllItems', function (DataServ) {
    return {
        restrict: 'E',
        scope: {},
        link: function (scope, elem, attrs) {
            scope.values = DataServ.currentTemplate.getAllItems();
        },
        template: '<div ng-repeat="item in values">{{item}}<render-item render="item"></renderItem></div>'
    };
});

This directive supposedly iterates the list of elements and render them one by one. 据推测,该指令对元素列表进行迭代并逐一渲染它们。 The single Item render is working after a initial typo correction. 最初的错字校正后,单项渲染正在工作。

The output on the modal is: 模态上的输出为:

[[
Item order = and item value =

]]
Item order = and item value =

And is always the same output, no matter how many items are on the array. 而且,无论阵列上有多少项,输出始终是相同的。

My main goal is easy: I should be able to add as many items I want using the textbox and then when I press the open modal, I should be able to see the list of elements rendered in the modal dialog. 我的主要目标很容易:我应该能够使用文本框添加任意数量的项目,然后在按下打开的模态时,我应该能够看到模态对话框中渲染的元素列表。

I would really appreciate guidance on where I am doing it wrong to achieve the result I want. 我非常感谢在错误的地方提供指导,以实现所需的结果。

Thanks in advance. 提前致谢。

You have a typo on this line: 您在此行上有错别字:

    <div><pre><render-item render="template.createTemplateItem(textBoxData)"></render-item></pre>

Should be: 应该:

    <div><pre><render-item render="template.CreateTemplateItem(textBoxData)"></render-item></pre>

This segment of code: 此段代码:

Template.prototype.getAllItems = function () {
    //take a template item object and add it to 
    //the template items repository
    return JSON.stringify(this.items);
};

is called once by your renderAllItems directive when it links: 链接时,您的renderAllItems指令会调用一次:

link: function (scope, elem, attrs) {
    scope.values = DataServ.currentTemplate.getAllItems();
},

All changes to that template's items array are not reflected in the directive because you JSON.stringify 'd the array. 对该模板的items数组的所有更改都不会反映在指令中,因为您对数组进行JSON.stringify

https://jsfiddle.net/urq3gu5o/ https://jsfiddle.net/urq3gu5o/

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

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