美文网首页
2020-09-25 angularjs 开发笔记

2020-09-25 angularjs 开发笔记

作者: rub1cky | 来源:发表于2020-09-25 09:43 被阅读0次

    scope 符号含义

    < one-way binding/单项绑定

    = two-way binding/双向绑定

    & function binding/绑定函数

    @ pass only strings/绑定字符

    自定义组件的ng-model

    代码

    <form name="myForm">
            <input type="text" ng-model="foobar">
            <my-directive ng-model="foobar"></my-directive>
        </form>
        <br>myForm.$dirty = {{myForm.$dirty}}
        <br>myForm.$pristine = {{myForm.$pristine}}
        <br><button ng-click="myForm.$setDirty()">Set dirty</button>
        <br><button ng-click="myForm.$setPristine()">Set pristine</button>
    
    angular.module('myModule', [])
    .directive('myDirective', function() {
      return {
        restrict: 'E',
        require: { ngModelCtrl: 'ngModel' },
        scope: {
          ngModel: '<'
        },
        bindToController: true,
        controllerAs: '$ctrl',
        template: 
           `<div>
              <button ng-click="$ctrl.ngModelCtrl.$setViewValue('foo')">
                  Set foo
              </button>
              <button ng-click="$ctrl.ngModelCtrl.$setViewValue('bar')">
                  Set bar
              </button>
           </div>`,
        controller: function ctrl() {}
      };
    });
    

    相关文章

      网友评论

          本文标题:2020-09-25 angularjs 开发笔记

          本文链接:https://www.haomeiwen.com/subject/hiwxuktx.html