AngularJS链接功能不叫 [英] AngularJS Link function not called

查看:133
本文介绍了AngularJS链接功能不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写我的第一个AngularJS指令:一是涉及链接功能。该指令被加载,但是当我使用它在我的页面中的链接函数没有被调用。

I'm trying to write my first AngularJS directive: one involving the link function. The directive is being loaded, but when I use it in my page the link function is not called.

结果

下面是小提琴: http://jsfiddle.net/jCUSh/115/

这里是HTML:

<div ng-app="biApp">
    <google-maps-symbol></google-maps-symbol>
</div>

和JavaScript的:

and the JavaScript:

var appModule = angular.module('biApp', []);

appModule.directive('googleMapsSymbol', function () {
    console.log("Directive was run");
    return {
        link: function (scope, elem, attrs) {
            console.log("Link was called");
        }
    };
});

结果

我敢打赌,我在做一些简单的事情是错误的。

I'll bet I'm doing some simple thing wrong.

推荐答案

有关角的默认值是假设指令是属性,而不是元素!您正在使用指令作为一个元素,所以你需要与限制指定。更新后的code读取:

The default for angular is to assume that directives are attributes, not elements! You are using a directive as an element so you need to specify this with the restrict. The updated code reads:

appModule.directive('googleMapsSymbol', function () {
    console.log("Directive was run");
    return {
        restrict: 'E',
        link: function (scope, elem, attrs) {
            console.log("Link was called");
        }
    };
});

请注意在限制:'E',。祝你好运!

更新您的提琴: http://jsfiddle.net/j8ZZ4/

这篇关于AngularJS链接功能不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆