将Javascript变量传递到指令参数中
我有一个Javascript变量,我想通过元素指令传递它。 我是在将Javascript变量传递到指令参数中,javascript,angularjs,Javascript,Angularjs,我有一个Javascript变量,我想通过元素指令传递它。 我是在ng init的帮助下完成的,但我的客户告诉我这将导致他们的ServiceNow设置出现问题,所以我必须通过指令参数来完成 我每次尝试都会出错。这是我的密码: APP.JS //directive decleration app.directive('myDirective', function(){ var directive = {}; directive.restrict = 'E'; directive.sc
ng init
的帮助下完成的,但我的客户告诉我这将导致他们的ServiceNow设置出现问题,所以我必须通过指令参数来完成
我每次尝试都会出错。这是我的密码:
APP.JS
//directive decleration
app.directive('myDirective', function(){
var directive = {};
directive.restrict = 'E';
directive.scope = {
myParam: '='
};
directive.template = '<div>{{myParam}}</div>';
return directive;
}) ;
//指令减容
app.directive('myDirective',function(){
var指令={};
directive.restrict='E';
指令范围={
myParam:“=”
};
directive.template='{myParam}}}';
返回指令;
}) ;
HTML
var temporary_var='Helloworld';
如你们所见,我现在刚刚传递了一个普通字符串,但我将通过这个指令参数传递数组或对象。这只是一个例子。我也尝试过传递数组和对象,但运气不好。试试这个:
<script>
var temporary_var = 'Helloworld';
</script>
<div ng-controller="progressController">
<my-directive my-param="param"></my-directive>
</div>
var temporary_var='Helloworld';
angular.module('components',[]).directive('myDirective',function(){
返回{
限制:'E',
范围:{
myParam:“=”
},
模板:“Hi{{myParam}}”
}
});
角度.module('myApp',['components']).controller('progressController',['$scope','$window',函数($scope,$window){
$scope.param=$window.temporary_var;//这是键
}]);
演示:好的,所以基本上我必须使用$window。谢谢
<script>
var temporary_var = 'Helloworld';
</script>
<div ng-controller="progressController">
<my-directive my-param="param"></my-directive>
</div>
angular.module('components', []).directive('myDirective', function() {
return {
restrict: 'E',
scope: {
myParam: '='
},
template: '<div>Hi {{myParam}}</div>'
}
});
angular.module('myApp', ['components']).controller('progressController', ['$scope', '$window', function($scope, $window) {
$scope.param = $window.temporary_var; //This is the key
}]);