我在隔离的作用域中有一个指令,我在指令的链接函数中定义了几个变量,因此模板无法绑定到这些变量,但模板URL似乎很容易绑定到这些变量...这是什么原因呢?
mymodule.directive("directive1", function () {
return {
require: '^widget',
// template: '<div><h1 > {{editMessage}}</h1></div>',
templateUrl: "dummy.htm",
scope: {
},
link: function (scope, element, attrs) {
scope.editMessage = "Click to edit";
});
}
});
我的虚拟.htm具有与模板内容相同的代码段:-
<div><h1 > {{editMessage}}</h1></div>
当我使用 templateUrl 时会发生绑定,但当我使用模板时不会发生绑定,为什么会这样? 两者的执行流程是否不同?
问题是你没有很好地集成范围变量,它应该是,这个例子:
mymodule.directive('myAlertaSeguimientoButton', function() {
return {
restrict: 'A',
scope: {
myParamElemento: '=',
myParamUrl: '@',
myParamSeguir: '@',
myParamDejarSeguir: '@',
myParamSiguiendo: '@',
myParamRefrescaMapa: '@'
},
template: '<button ng-class="(!myParamElemento.siguiendo)?'btn btn-default seguir':'btn btn-default seguir siguiendo activeButtonAzul'" class="btn btn-default seguir" ng-click="seguirNoSeguir(myParamUrl)"><span class="icon-seguir estadoUno">[[myParamSeguir]]</span><span class="icon-siguiendo icon-w estadoDos">[[myParamSiguiendo]]</span><span class="estadoTres">[[myParamDejarSeguir]]</span></button>',
controller: ['$scope', 'Service', function($scope, Service) {...
在 html 中:
<span my-param-elemento="angular-var" my-param-url="angular-var" my-alerta-seguimiento-button></span>