简体   繁体   English

如何保存ng-repeat中的元素?

[英]How can I save an element from an ng-repeat?

I am showing elements with ng-repeat and I want to select one of the elements then show the selected element in another div page or whatever. 我正在显示具有ng-repeat元素,我想选择一个元素,然后在另一个div页面或其他内容中显示选定的元素。

    <div ng-show="newsignature" id="newsignature">
    <div class="ng-cloak">
    <div ng-click="showcreateform()" ng-hide="createsignature" class="ncontact" ng-repeat="template in signaturetemplates">
    <h2>{{template.name}}</h2>
    <div class="tdescrip" data-ng-bind-html="template.templatesample"></div>
    <br>
    </div>
  </div>
  </div>

The elements that I want to preserve are the name and the html but I don't know how to do it. 我要保留的元素是名称和html,但我不知道该怎么做。

When i select on of the elements i show a form with an input fields, at the top of the form i have a preview button, the idea when i clicked is to show a preview of the template selected with the info given in the inputs. 当我选择其中一个元素时,我会显示一个带有输入字段的表单,在表单的顶部,我有一个预览按钮,当我单击时,其想法是显示在输入中提供的信息所选择的模板的预览。 I can't get or preserve the template (is an HTML) in the first step to show in the preview. 在第一步中,我无法获取或保留模板(是HTML)以显示在预览中。

<div ng-show="createsignature">
  <h2  ng-model="newSignature.name" placeholder="Name this Signature" contenteditable="true">Name this signature </h2>
 <span id="cancel" class="pull-right cancel cancels" ng-click="createsignature=false"><button>CANCEL</button></span>
 <span ng-click="showpreview(); createsignature=false; newsignature=false;"id="save" class="pull-right cancel" ><button>PREVIEW</button></span>
  <div class="pull-left">
      <span id="addsi" ng-click="createsignature=false;" class="pull-left"><img src="images/b-a-c-k-btn.png"><h2> New Signature</h2></span>
    </div>
    <table class="spacing-table table ">
    <tr>
        <td><input class="fill" ng-model="echo_Name" contentEditable="true" placeholder="Name"></input></td>
        <td></td>
        <td></td>
    </tr>
     <tr>
        <td><input class="fill" ng-model="echo_Title" contentEditable="true" placeholder="Title"></input></td>
        <td></td>
        <td></td>
    </tr>
     <tr>
        <td><input class="fill" ng-model="echo_Department" contentEditable="true" placeholder="Department"></input></td>
        <td></td>
        <td></td>
    </tr>
     <tr>
        <td><input class="fill" ng-model="echo_Company" contentEditable="true" placeholder="Company"></input>
        </td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td><input class="fill" ng-model="echo_Phone" contentEditable="true" placeholder="Mobile Number"></input>
        </td>
        <td></td>
         <td> <a href=""><img src="images/p-l-u-s-off-btn-copy.png"></a></td>
    </tr>
    <tr>    
         <td><input class="fill" ng-model="echo_Phone" contentEditable="true" placeholder="Office Number"></input></td>
        <td></td>
        <td> <a href=""><img src="images/p-l-u-s-off-btn-copy.png"></a></td>
    </tr>

    <tr>    
         <td><input class="fill" ng-model="echo_Phone" contentEditable="true" placeholder="Fax Number"></input></td>
        <td></td>
        <td> <a href=""><img src="images/p-l-u-s-off-btn-copy.png"></a></td>
    </tr>

    <tr>    
         <td><input class="fill" ng-model="echo_Email" contentEditable="true" placeholder="Email"></input></td>
        <td></td>
        <td> <a href=""><img src="images/p-l-u-s-off-btn-copy.png"></a></td>
    </tr>

    <tr>    
         <td><input class="fill" ng-model="echo_Website" contentEditable="true" placeholder="Website"></input></td>
        <td></td>
        <td> <a href=""><img src="images/p-l-u-s-off-btn-copy.png"></a></td>
    </tr>

    <tr>
        <td><input class="fill" ng-model="echo_Street" contentEditable="true" placeholder="Street Addres"></input></td>
        <td></td>
        <td></td>
    </tr>

     <tr>
        <td><input class="fill" ng-model="echo_Postal" contentEditable="true" placeholder="Postal Addres"></input></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td><input class="fill" ng-model="echo_Social"  contentEditable="true" placeholder="Social"></input></td>
        <td></td>
        <td></td>
    </tr>

  </table>
  </div>

If I understand you correctly you want to do something with one element after an event like a user click. 如果我对您的理解正确,那么您想在用户单击之类的事件后对一个元素执行某项操作。

<div ng-repeat="template in signaturetemplates">
    <h2 ng-click="selectedTemplate=template">{{template.name}}</h2>
</div>

//Somewhere else in the code...
<div ng-show="selectedTemplate">
    <h2>Selected {{selectedTemplate.name}}</h2>
    <pre>{{selectedTemplate.templatesample}}</pre>
<div>

One way is to create a service which you can then access from other controllers, directives or whatever by simply injecting it. 一种方法是创建一个服务,然后可以通过简单地注入它来从其他控制器,指令或其他任何东西进行访问。

First create the service which will hold the values: 首先创建将包含值的服务:

core.service('shared', [function() {

  var myObj;

  return {
    setObj: function(value) {
      myObj = value;
    },
    getObj: function() {
      return myObj;
    }
  }
}]);

Then create your save function: 然后创建您的保存功能:

$scope.saveObj = function(myObj) {
  shared.setObj(myObj);
};

And apply it to a ng-click event: 并将其应用于ng-click事件:

Then you can use the getObj function to fetch the object from another controller for example: 然后,您可以使用getObj函数从另一个控制器获取对象,例如:

core.controller('SomeOtherCtrl', ['shared', function(shared) {
  $scope.myObj = shared.getObj();
}]);

And use it in your template: 并在模板中使用它:

<div>{{myObj.name}}</div>

If you also want to preserve the HTML I'd suggest you create a directive for it which will render the necessary HTML which you can then add your $scope variable to. 如果您还想保留HTML,建议您为其创建一个指令,该指令将呈现必要的HTML,然后可以将$scope变量添加到其中。

core.directive('myDirective', [function() {
  return {
    restrict: 'A',
    scope: {
      myObj: '='
    },
    template: '<div>{{myObj.name}}</div>'
  }
}]);

Then use it like so: 然后像这样使用它:

<myDirective myObj="myObj"></myDirective>

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

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