简体   繁体   English

如何将HTML元素传递到angularjs代码中?

[英]How to pass HTML element into angularjs code?

I want to pass the html element into angularjs code. 我想将html元素传递给angularjs代码。 I have some html elements like data-form-selector='#linechart_general_form' and data-url="{% url 'horizon:admin:metering:samples'%}" I want to access these Html elements in angularjs code. 我有一些html元素,例如data-form-selector='#linechart_general_form'data-url="{% url 'horizon:admin:metering:samples'%}" data-form-selector='#linechart_general_form' data-url="{% url 'horizon:admin:metering:samples'%}"我想在angularjs代码中访问这些HTML元素。

My html templates, what make to change in the following code so that its elements accessible to angularjs code. 我的html模板,在以下代码中进行了哪些更改,以便angularjs代码可以访问其元素。

<div class="info row detail">
      <div class="col-sm-9 chart_container">
        <div class="chart"
             data-chart-type="line_chart"
             data-url="{% url 'horizon:admin:metering:samples'%}"
             data-form-selector='#linechart_general_form'
             data-legend-selector="#legend"
             data-smoother-selector="#smoother"
             data-slider-selector="#slider">
        </div>
        <div id="slider"></div>
        <div class="col-sm-3 legend_container">
          <div id="smoother" title="Smoothing"></div>
          <div id="legend"></div>
        </div>
      </div>
    </div>

--Update-- I just reread your question. -更新-我只是重新阅读你的问题。 To access those ATTRIBUTES (not elements, they are values of an element) you can select the object with an identifier, then manipulate it's attributes, like so (jQuery example): $('.chart').attr('data-form-selector', 'myNewValue'); 要访问那些属性(不是元素,它们是元素的值),您可以选择带有标识符的对象,然后操纵它的属性,就像这样(jQuery示例):$('。chart')。attr('data-form -selector','myNewValue');

You may need to re-initialize sopmething because of the databinding but that's where I'd start. 由于数据绑定,您可能需要重新初始化Sopmething,但这就是我要开始的地方。

================================== =================================

You need to create an angular app and controller first, then use the angularjs and ng-model directive to bind the element to a method in your controller, ie: 您需要首先创建一个angular应用程序和控制器,然后使用angularjs和ng-model指令将元素绑定到控制器中的方法,即:

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

In this case, you'd be manipulating the $scope.firstName and $scope.lastName variables in js. 在这种情况下,您将在js中操作$ scope.firstName和$ scope.lastName变量。

Courtesy W3Schools http://www.w3schools.com/angular/angular_intro.asp W3Schools提供http://www.w3schools.com/angular/angular_intro.asp

You can do a Directive for doing that. 您可以执行此指令。

https://docs.angularjs.org/guide/directive https://docs.angularjs.org/guide/directive

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

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