如何在Angular Js的指令的link部分中访问变量?

时间:2015-07-11 17:53:07

标签: javascript angularjs angularjs-directive

我正在尝试创建一个指令,该指令将生成当前的版权年份。如何访问指令链接中的年份变量?我已经尝试了一切,但没有任何作用。

.directive('copyrightYear', function() {
    return {
        restrict: 'E',
        link: function(scope, element) {
            var date = new Date();
            var year = date.getFullYear();
        },
        template: ''
    };
})

1 个答案:

答案 0 :(得分:4)

将其添加到范围

 module.exports = function (grunt) {

 grunt.initConfig({
     pkg: grunt.file.readJSON('package.json'),

     less: {
         development: {
             options: {
                 compress: false
             },
             files: {
                 "css/unsorted_style.css": "less/style.less"
             }
         }
     },

     csscomb: {
        foo: {
             files: {
                 'css/style.css': ['css/unsorted_style.css']
             }
        }
     },

     watch: {
         less: {
             files: ['less/*.less'],
             tasks: ['less'],
             options: {
                 spawn: false
             }
         },

         css {
            files: ['css/unsorted_style.css'],
            tasks: ['css', 'csscomb'],
            options: {
                 spawn: false
             }
         }

     }
 });

 grunt.loadNpmTasks('grunt-contrib-less');
 grunt.loadNpmTasks('grunt-csscomb');
 grunt.loadNpmTasks('grunt-contrib-watch');

 grunt.registerTask('default', ['less', 'csscomb', 'watch']);

 };