通过NG-初始化变量注入到控制器 [英] Inject variables via ng-init to controller
本文介绍了通过NG-初始化变量注入到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用不同的值的倍数倍相同的变量注入到相同的控制器。这是我试过。什么是方式每次调用得到的值不同?
HTML
<机身NG-应用=对myApp>
< DIV NG控制器=myCtrlNG-的init =测试='的HelloWorld'; TEST1 ='helloworld2'> < / DIV>
< DIV NG控制器=myCtrlNG-的init =测试='helloworld3'; TEST1 ='helloworld4'> < / DIV>
< DIV NG控制器=myCtrlNG-的init =测试='helloworld5'; TEST1 ='helloworld6'> < / DIV>
<身体GT;
的JavaScript code
VAR应用= angular.module(对myApp,[]);app.controller(myCtrl,[$范围功能($范围){
的console.log($ scope.test);
的console.log($ scope.test1);
}]);
解决方案
找到一个肮脏的修复。感谢所有为您的输入。
VAR应用= angular.module(对myApp,[]);app.controller(myCtrl,[$范围,$超时',函数($范围,$超时){
$超时(函数(){
的console.log($ scope.test);
的console.log($ scope.test1);
},1);
}]);
I want to inject the same variables with different values multiples times to the same controller. This is what I tried. What is a way to get different values in each call?
HTML
<body ng-app="myApp">
<div ng-controller="myCtrl" ng-init="test='helloworld';test1='helloworld2'">
</div>
<div ng-controller="myCtrl" ng-init="test='helloworld3';test1='helloworld4'">
</div>
<div ng-controller="myCtrl" ng-init="test='helloworld5';test1='helloworld6'">
</div>
<body>
JavaScript code
var app = angular.module("myApp", []);
app.controller("myCtrl", ["$scope",function($scope) {
console.log($scope.test);
console.log($scope.test1);
}]);
解决方案
Found a dirty fix. Thanks all for your inputs.
var app = angular.module("myApp", []);
app.controller("myCtrl", ["$scope", '$timeout',function($scope, $timeout) {
$timeout(function(){
console.log($scope.test);
console.log($scope.test1);
}, 1);
}]);
这篇关于通过NG-初始化变量注入到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文