我是 AngularJS JavaScript 新手。刚开始学它。我正在尝试一些小示例程序。这是我尝试过的,但它抛出错误。
<body ng-app="myApp">
<div ng-controller="myCtrl">
{{time}}
<br>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.service('hexafy', ['',
function() {
this.myfunc = function(num) {
return num.toString(16);
}
}
]);
app.controller('myCtrl', ['hexafy', '$scope', '$interval',
function(hexafy, $scope, $interval) {
$scope.time = new Date().toLocaleTimeString();
$interval(function() {
$scope.time = new Date().toLocaleTimeString();
}, 1000);
// $scope.hex = hexafy.myfunc(255);
}
]);
</script>
最佳答案
当代码被缩小并保留参数的映射时,使用数组语法。有关更多信息,请参阅我的另一个答案 Why we Inject our dependencies two times in angularjs? .
代码中,由于没有向hexafy
服务传递任何参数,因此不需要使用数组语法并传递空字符串。
app.service('hexafy', ['',
function() {
使用正常语法。
app.service('hexafy', function() { // <-- Remove `[` and empty string from here
...
...
}); // <-- Remove `]` from here
var app = angular.module("myApp", []);
app.service('hexafy', function() {
this.myfunc = function(num) {
return num.toString(16);
}
});
app.controller('myCtrl', ['hexafy', '$scope', '$interval',
function(hexafy, $scope, $interval) {
$scope.time = new Date().toLocaleTimeString();
$interval(function() {
$scope.time = new Date().toLocaleTimeString();
}, 1000);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="myCtrl">
{{ time }}
<br />
</div>
</body>
关于javascript - AngularJS 中的 $injector.unpr 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685341/