Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs element指令中的Angular.JS绑定属性_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs element指令中的Angular.JS绑定属性

Angularjs element指令中的Angular.JS绑定属性,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我想做的是创建一个元素指令,它可以具有可绑定属性,并且可以处理静态值 我有一个指令myTag,它应该支持启用/禁用某些功能。。。我想让它像你一样工作 <my-tag enable_f1="true" enable_f2="true" /> 或者 <my-tag enable_f1="{{mc.someVal1}}" enable_f2="{{mc.someVal2}}" /> 现在我如何编写link方法来支持绑定到属性和静态值 angular.module(

我想做的是创建一个元素指令,它可以具有可绑定属性,并且可以处理静态值

我有一个指令myTag,它应该支持启用/禁用某些功能。。。我想让它像你一样工作

<my-tag enable_f1="true" enable_f2="true" />

或者

<my-tag enable_f1="{{mc.someVal1}}" enable_f2="{{mc.someVal2}}" />

现在我如何编写link方法来支持绑定到属性和静态值

 angular.module('TestModule',[])
  .directive('myTag',function() {
      return {
          restrict: 'E',
          templateUrl: '<div></div>',
          link: function (scope, element, attrs){
                 //I can get attrs.enable_f1, attrs.enable_f2, but what if it is bound to model? 
          }
      }
  });
angular.module('TestModule',[])
.directive('myTag',function(){
返回{
限制:'E',
templateUrl:“”,
链接:函数(范围、元素、属性){
//我可以得到attrs.enable_f1,attrs.enable_f2,但如果它绑定到模型呢?
}
}
});

您可以有一个独立的作用域来获取以下值:

HTML:


指令:

myApp.directive('myTag',function() {
      return {
          restrict: 'E',
          template: '<div></div>',
          scope: {
              enableF1: '=',
              enableF2: '='
          },
          link: function (scope, element, attrs){
                 console.log(scope.enableF1);
                 console.log(scope.enableF2);
          }
      }
  });
myApp.directive('myTag',function(){
返回{
限制:'E',
模板:“”,
范围:{
enableF1:“=”,
enableF2:“=”
},
链接:函数(范围、元素、属性){
console.log(scope.enableF1);
console.log(scope.enableF2);
}
}
});

希望有一天我能使用这个范围:)非常感谢,我将尝试如何更改enableF1和enableF2?我应该用手表吗?例如,当它被绑定,并且在模型上被更改时,如何在指令中获得通知?如果我使用watch,如果值是静态的,是否可以?哦,我看到它与watch一起工作,非常感谢您的回答@ArsenMkrtchyan Yes,
$scope的值。enableF1
将自动更改,祝您好运!
myApp.directive('myTag',function() {
      return {
          restrict: 'E',
          template: '<div></div>',
          scope: {
              enableF1: '=',
              enableF2: '='
          },
          link: function (scope, element, attrs){
                 console.log(scope.enableF1);
                 console.log(scope.enableF2);
          }
      }
  });