标签: angularjs-directive

如何使用 Angular JS 在输入字段中输入时将文本转换为大写

我知道这对于 jquery 是可能的,但我不知道如何使用 Angular js 做到这一点,请问有什么建议吗?

       function mayuscula(campo){
            $(campo).keyup(function() {
                           $(this).val($(this).val().toUpperCase());
            });
       }
Run Code Online (Sandbox Code Playgroud)

angularjs-directive

1
推荐指数
1
解决办法
3万
查看次数

忽略元素编译的指令

我想编写一个名为 的指令ng-ignore来阻止 HTML 编译器处理元素及其子元素。

我一直在练习编写指令,但对此有点困惑。


我想要这样的东西:

angular.module("myMod", [])
.directive("ngIgnore", function()
{
    var dirDefObj = {
        // What should I write here to have compiler ignore this element?
    };
    return dirDefObj;
});
Run Code Online (Sandbox Code Playgroud)

像这样使用:

<div ng-ignore>
    This div must be ignored by Angular's compiler.
</div>
Run Code Online (Sandbox Code Playgroud)

需要帮助和指导来编写它。谢谢。

angularjs angularjs-directive

1
推荐指数
1
解决办法
2418
查看次数

如何从外部调用指令函数?

我有一个指令可以为我生成一个列表。该指令具有分页方法。我想使用键盘控制此列表,以便当我向左或向右按​​列表页面的下一页或上一页时。

反正我能做到吗?

下面是我的指令代码:

app.directive("gridview", function(){
    return {
        restrict:'E',  
        transclude: true,           
        template:'<div><button ng-disabled="!hasPrevious()" ng-click="onPrev()"> Previous </button><button ng-disabled="!hasNext()" ng-click="onNext()"> Next </button></div><div ng-transclude></div>',
        scope:{ 
            'currentItem':'=',
            'currentPage':'=',
            'pageLimit':'=',
            'data':'=',
            'totalPages' :'&'
        },
        link:function($scope, element, attrs){

            $scope.size = function(){
                return angular.isDefined($scope.data) ? $scope.data.length : 0;
            };

            $scope.end = function(){
                return $scope.start() + $scope.pageLimit;
            };

            $scope.start = function(){
                return $scope.currentPage * $scope.pageLimit;
            };

            $scope.totalPages = function(){
                $scope.totalPages({theTotal : $scope.size});
            };

            $scope.page = function(){
                return !!$scope.size() ? ( $scope.currentPage + 1 ) : 0;
            };

            $scope.hasNext = …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

1
推荐指数
1
解决办法
4076
查看次数

我可以强制一个元素显示其悬停效果吗?

我正在 angularjs 中创建一个表格分页指令,并且我正在使用引导程序按钮来显示页码。由于我正在制定此指令以用于工作并可能与多个应用程序一起使用,因此我将其设置为可定制的。因此,我按钮上的所有类都绑定到我作用域上的变量(基本上允许用户为每个按钮定义自己的类)。我的问题是,由于他们可以定义按钮的类,因此我无法为它们当前所在的页码的类设置具体颜色。因此,例如,如果他们单击第 3 页,我希望更改第 3 页,以便他们知道自己在哪个页面上。当您将鼠标悬停在每个引导按钮上时使用的颜色可以完美地工作,但仅当您将鼠标悬停在它们上时才会应用。

这是我想使用 ng-class 应用悬停类的地方(在 paginatedTable.html 中):

<button title="Page {{pageNumber}}" type="button" class="{{cndBtnNumbersClass}}" ng-repeat="pageNumber in cndPageNumbers"
        ng-click="cndTablePaginationNav(pageNumber)">{{pageNumber}}</button>
Run Code Online (Sandbox Code Playgroud)

这是一个工作plnkr。如果您单击其中一个页码,它会在聚焦时应用该样式,但是一旦您单击关闭,它就不再显示选择了哪个页码。

http://plnkr.co/edit/CHO6HbT4emM3VEwcNnzq?p=preview

预先感谢您的任何帮助!

javascript css twitter-bootstrap angularjs angularjs-directive

1
推荐指数
1
解决办法
2228
查看次数

在 Angular UI Modal 中集成指令

在这个plunk我有以下内容:

  • 在控制器和指令之间共享的控制对象。
  • 该指令在控制对象中声明了一个方法。
  • 控制器调用方法来设置一个值。
  • 该指令包含在 Angular UI Modal 中。

由于某种原因,打开模态时控制对象为空(查看控制台日志)。如何从控制器调用方法来设置字段值?

HTML

<div ng-app="app" ng-controller="myCtl">

        <button ng-click="openModal()">Open modal</button>

        <script type="text/ng-template" id="myModalContent.html">

            <div class="modal-header">
                <h4 class="modal-title">The Title</h4>
            </div>

            <ddl control="ddlControl"></ddl>                

            <div class="modal-footer">
                <button type="submit">Submit</button>
            </div>

       </script>

    </div>
Run Code Online (Sandbox Code Playgroud)

Javascript

var app = angular.module('app', ['ui.bootstrap']);
app.controller('myCtl', function($scope,$uibModal) {

        $scope.ddlControl = {};

        $scope.openModal = function() {
          $scope.modalInstance = $uibModal.open({
              templateUrl: 'myModalContent.html',
              scope: $scope
            }); 

          console.log($scope.ddlControl);
          $scope.ddlControl.set();

        };
})

.directive('ddl', function () {

    var directive = {};
    directive.restrict = 'EA';
    directive.scope = {
         control: …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

1
推荐指数
1
解决办法
3020
查看次数

在angularjs中将字符串转换为数字

我正在显示一个包含 JSON 的数组,并直接用 HTML 显示它,在这里你可以找到elem.fc + elem.cpc. 它将 NaN 作为输出。

这是我的 HTML:

<tr ng-show="table.table1loaded" ng-repeat="elem in filteredcampaignslist" ng-init="initializepopover()">
<td ng-show="app.ostype=='iOS'"><div class="text-center">{{elem.fc+elem.cpc}}</div></td>
</tr>
Run Code Online (Sandbox Code Playgroud)

angularjs-directive angularjs-ng-repeat

1
推荐指数
2
解决办法
4万
查看次数

ng-if only 第一个指令与自关闭标签一起使用

我是角度新手,所以这可能不是最好的方法。随着我了解更多,我可能会重做大部分已经做过的事情。

我正在使用 url 路由,ngRoute它工作得很好。在其中一个页面中,我有许多 ng-include 模板 - 再次,一切都很好。

在其中一个ng-include模板中,我使用两个directives来显示模板。

<provider-search providers="practiceData.providers" states="ref.states" specialties="ref.specialties" status="updateSearchStatus" ng-show="searchStatus == 1"/>
<search-list status="updateSearchStatus" addp="addProvider" providers="searchResults" ng-if="searchStatus == 2" />
Run Code Online (Sandbox Code Playgroud)

每个指令都使用独立的作用域和控制器。问题是只有第一个指令有效。如果我切换顺序,则第二个指令(现在如果是第一个位置)将起作用。简而言之,只有首先列出的指令才有效。

任何帮助表示赞赏。

directive angularjs angularjs-directive angular-ng-if

1
推荐指数
1
解决办法
292
查看次数

指令/组件名称“ng”无效。名称不应包含前导或尾随空格

尝试在生产中运行我的应用程序并收到一个奇怪的错误

代码/错误消息

angular.js:68Uncaught Error: [$injector:modulerr] Failed to instantiate module ng due to:
Error: [$compile:baddir] Directive/Component name 'ng ' is invalid. The name should not contain leading or trailing whitespaces
Run Code Online (Sandbox Code Playgroud)

我查看了模块"ng "但没有找到

是否有熟悉的问题?

运行 。我的 Docker 文件中的节点 6.2.2

多谢

javascript angularjs angularjs-directive

1
推荐指数
1
解决办法
757
查看次数

AngularJs - 使用指令在点击事件上显示隐藏密码

我现在有一个密码输入字段和一个带有该输入字段的按钮
,如果输入字段的类型是密码,即type="password",然后单击该按钮,它应该变成文本type="text"
即如果我再次单击同一个按钮,它应该更改type="password"

意味着它应该切换value输入元素的类型

我已经使用控制器完成了此操作,它与控制器一起工作正常。下面是使用控制器的工作代码

但是如果我想使用指令而不是控制器,那么如何使用指令处理此切换条件

目的 - 我想在多个输入元素上使用此功能

超文本标记语言

<div class="input-group">
   <label>Password</label>
   <input type="{{inputType}}" class="form-control" ng-model="password" />
   <span ng-click="show()">show</span>
</div>
Run Code Online (Sandbox Code Playgroud)

控制器

$scope.inputType = 'password';

$scope.show = function() {
  if ($scope.inputType === 'password') {
     $scope.inputType = !$scope.inputType;
     $scope.inputType = 'text';
  } else {
     $scope.inputType = !$scope.inputType;
     $scope.inputType = 'password';
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用指令 - 下面是我的试用代码,
我不知道如何<input />使用指令更改元素的类型

指示

.directive('myDir', function() {
    require: 'ngModel',
    link: function (scope, element, attrs) { …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive

1
推荐指数
1
解决办法
3039
查看次数

如何解决指令“ngChange”所需的控制器“ngModel”无法找到!例外?

我知道上面的问题有很多答案,用小差异“指令”代替 ngChange。我正在使用 1.7.2 版本。这是我的代码

<select class="full-width" id="searchWithSavedFilter" ng-modal="searchWithSavedFilter" ng-change="test(searchWithSavedFilter)">
 <option ng-repeat="p in SavedFilters" value="{{p.Id}}">{{p.Name}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

和 angularjs 参考代码是

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.2/angular.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive

1
推荐指数
1
解决办法
1868
查看次数