How to write directive on class in Angular Js?

后端 未结 2 1602
夕颜
夕颜 2020-12-07 02:19

The restrict option is typically set to:

\'A\' - only matches attribute name
\'E\' - only matches element name
\'C\' - only matches class name
\'M\' - on

2条回答
  •  被撕碎了的回忆
    2020-12-07 02:49

    Some issues in your code:

    • Isn't necessary use scope: { ngModel: '='}. Instead use require.
    • For access to the value of the ngModel value passed like 4th argument, use ngModel.$viewValue.
    • If you need access to the variable of your controller, you have 2 posibilities.
      1. remove the scope property for get a shared scope with the controller, and access directly to the answers variable through the scope passed in the link function like this scope.answers.
      2. This it's the better option. sharing access to the answers variables within the isolated scope property of your directive. scope: { answers: '=' }

    The modified code:

    function validVehicleyear($http) {
        return {
          restrict: 'C',
          require: 'ngModel',
          link: function (scope, element, attrs, ngModel) {
            element.bind('change', function () {
              console.log('here in validVehicleyear');
              $http.get('api.php' + ngModel.$viewValue)
                .then(function (response) {
                  scope.answers.VehicleMake = response.data;
                });
              });
            }
          }
        }
    

    My final recommendation it's not use the Class option, in your case, it's better use the Attribute option. Good luck!

提交回复
热议问题