Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 角度-如何构建动态添加的控件?_Jquery_Angularjs_Ui Select2 - Fatal编程技术网

Jquery 角度-如何构建动态添加的控件?

Jquery 角度-如何构建动态添加的控件?,jquery,angularjs,ui-select2,Jquery,Angularjs,Ui Select2,我是angular的新手,已经使用requirejs和angular建立了一个项目,但是用UI做了一些非常简单的事情 我的问题是使用angular,如何创建可以在运行时重用并添加到的组件/控件。例如,在jquery中,我可能会创建一个插件,并在对元素调用插件之前动态添加dom元素 i、 e。 $(el).append(“”).mycontrol({opts:1}) 我并没有真正开始使用角度模板和重复,但我认为这可能是一种方法,但如何“引导”动态添加的新控件 我想到的例子是,我正在使用ui-sel

我是angular的新手,已经使用requirejs和angular建立了一个项目,但是用UI做了一些非常简单的事情

我的问题是使用angular,如何创建可以在运行时重用并添加到的组件/控件。例如,在jquery中,我可能会创建一个插件,并在对元素调用插件之前动态添加dom元素

i、 e。 $(el).append(“”).mycontrol({opts:1})

我并没有真正开始使用角度模板和重复,但我认为这可能是一种方法,但如何“引导”动态添加的新控件

我想到的例子是,我正在使用ui-select2(AngularUI),我希望在一些动态添加到dom的面板中包含select2组件

当然,另一个解决方案是从添加主机dom元素的ng控制器调用select2函数


在angular应用程序中,添加的DOM元素不会自动解析。为此,您必须使用
$compile

见:


和$compile

哪些条件会使控件被包括或排除?如果控制器中有许多布尔变量用作标志,则可以使用ngShow/ngHide或ngIf指令来显示或隐藏它们。像下面这样的

<form>
    <input type="text" ng-model="input.name" ng-show="input_enabled.name">
    <input type="text" ng-model="input.age" ng-hide="input_enabled.age">
    <input type="email" ng-model="input.email" ng-if="input_enabled.email">
    <input type="text" ng-model="input.phone" ng-disabled="input_enabled.phone">
</form>
  • ngShow:创建并显示truthy值上的元素
  • ngHide:创建并隐藏truthy值上的元素
  • ngIf:在truthy value上创建元素,在falsy上销毁元素
  • ngDisabled:创建元素,然后向其添加disabled属性
这是一个非常基本的概述,不一定是你应该做事情的方式,只是一个如何做的例子。AngularJS文档非常优秀。角度是一个伟大的工具

这篇文章值得一读-
$scope.input = {
    name: '',
    age: '',
    email: '',
    phone: ''
};

$scope.input_enabled = {
    name: true, /* this will show input.name */
    age: false, /* this will show input.age */
    email: false, /* this will hide imput.email */
    phone: true /* this will disable, but not hide, input.phone */
};