Javascript 值为URL时,AngularJS([$parse:syntax]语法错误:Token';:';是位于的意外标记)
我已创建了如下指令:Javascript 值为URL时,AngularJS([$parse:syntax]语法错误:Token';:';是位于的意外标记),javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我已创建了如下指令: angular.module('mymodule') .directive('httpsimg', function () { return { restrict: 'E', //E = element, A = attribute, C = class, M = comment //@ reads the attribute value, = provides two-way binding, &
angular.module('mymodule')
.directive('httpsimg', function () {
return {
restrict: 'E', //E = element, A = attribute, C = class, M = comment
//@ reads the attribute value, = provides two-way binding, & works with functions
scope: {
style: '@',
width: '=',
height: '=',
'src': '='
},
template: '<img src="{{src}}" width="{{width}}" height="{{height}}" style="{{style}}" />',
link: function ($scope, element, attrs) {
var srcparts = $scope.src.replace('http','https');
$scope.src=srcparts;
} //DOM manipulation
}
});
<httpsimg src="http://www.mytest.com/imgdir/logo.png" width="50" />
scope: {
style: '@',
width: '=',
height: '=',
'src': '@'
}
它工作得很好,我的意思是我可以得到价值,没有错误。但是如果我将@to=更改为双向绑定,我会看到错误
我还发现问题是因为Url,所以如果我将Url更改为没有点和斜杠的简单世界,效果会很好。
=
双向绑定是绑定表达式,例如变量:
就像在Javascript中编写字符串文字一样。也许为了更好地说明这一点:
<httpsimg src="'http://' + 'www.mytest.com/imgdir/logo.png'">
因为您没有绑定变量表达式,所以使用双向绑定没有任何意义。如果您只想传递一个文本值,这正是
@
的目的。谢谢,这很有意义,实际上,我只想在内部指令模板中替换src,那么您需要编写src=“src”
,而不是{src}
。由于=
,使用{}
对其进行双重求值,src
属性已将作为表达式求值。我将模板更改为“”,将范围更改为@,但在检查结果时,src未被替换
<httpsimg src="'http://www.mytest.com/imgdir/logo.png'">
^ ^
<httpsimg src="'http://' + 'www.mytest.com/imgdir/logo.png'">