简体   繁体   中英

Custom directives is not working in Angular js

While running the following code it should be focused(like cursor on button) on the button named focussed shown here. But its not working

在此处输入图片说明

Html code :

<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 code :

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();
            }
        };
    });

How to fix this

You don't need to inject 'directives' into your module unless you intend to inject a new module for the directive later. Your controller should also be declared in a way similar to this example:

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

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

Just removing the ' directives ' you have included while declaring the module makes the code work. Also take care you have declared ng-App. I haven't included angular-resources.js but still it works for me. Have just included the angular.min.js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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