zoukankan      html  css  js  c++  java
  • [AngularJS] Using ngModel in Custom Directives

    You can use ngModel in your own directives, but there are a few things you'll need to do to get it working properly.

    ngModel itself is an directive. If you want to use it inside your own directive, you should use require keyword.

    /**
     * Created by Answer1215 on 12/18/2014.
     */
    angular.module('app', [])
        .directive('bank', function() {
            return{
                restrict: 'E',
                template: '<div>Click me to add $10 into your account</div>',
                require: 'ngModel',
                //The ^ prefix means that this directive searches for the controller on its parents (without the ^ prefix, the directive would look for the controller on just its own element)
                link: function(scope, element, attrs, ngModelCtrl) {
                    //so it looks for the controller on ngModel --> ngModelController
                    //it has method setViewValue
                    //has prop: viewValue
                    element.on('click', function() {
                        ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue + 10);
                        scope.$apply();
                    })
                }
            }
        })
    <!DOCTYPE html>
    <html ng-app="app">
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <div ng-init="money=10"></div>
    <bank ng-model="money"></bank>
    {{money | currency}}
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="app.js"></script>
    </body>
    </html>
  • 相关阅读:
    弹出框 popover.js
    模态框 modal.js
    关于css阴影和浮动
    css文件分类
    按钮效果
    二级导航栏
    在CSS3中,可以利用transform功能来实现文字或图像的旋转、缩放、倾斜、移动这四种类型的变形处理
    CSS3背景定位 background-origin
    什么是优雅降级和渐进增强
    居中
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4173059.html
Copyright © 2011-2022 走看看