簡體   English   中英

自定義指令在Angular JS中不起作用

[英]Custom directives is not working in Angular js

在運行以下代碼時,應將其聚焦(如按鈕上的光標)在此處顯示的名為focussed的按鈕上。 但是它不起作用

在此處輸入圖片說明

HTML代碼:

<body ng-controller="TextController">
 <button ng-click="clickUnfocused()">Not focussed</button>
 <button ngbk-focus ng-click="clickfocused()">focussed</button>
 <div>{{message.text}}</div>
</body>

JavaScript代碼:

var kitkatmodule = angular.module('Kitkat', ['directives']);

    kitkatmodule.controller('TextController',function($scope) {

         $scope.message = {text:"nothin clicked"};

         $scope.clickUnfocused = function() {
         $scope.message.text = "Click Unfocused";
         }
         $scope.clickfocused = function() {
         $scope.message.text = "clickfocused";
         }
    });

    kitkatmodule.directive('ngbkFocus', function(){
        return  {
            link : function(scope, element, attrs, controller) {
                element[0].focus();
            }
        };
    });

如何解決這個問題

您無需在模塊中注入“指令”,除非您打算稍后為指令注入新模塊。 還應該以類似於以下示例的方式聲明您的控制器:

function TextController($scope) {
    $scope.name = 'Superhero';
}

在使用指令時,我們需要添加angular-resource.js文件

只需在聲明模塊時刪除包含的“ directives ”即可使代碼正常工作。 還請注意您已聲明ng-App。 我沒有包含angular-resources.js但仍然對我有用。 剛剛包含了angular.min.js

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM