考虑以下视图模型:
$scope.data = {};
$scope.data.person = {};
$scope.data.person.firstname = "";
$scope.data.person.lastname = "";
$scope.data.person.username = "";
Run Code Online (Sandbox Code Playgroud)
以及以下元素指令:
<custom-form-directive ng-model="data.person"></custom-form-directive>
Run Code Online (Sandbox Code Playgroud)
其中包含三个输入标签来显示数据.如何使用量角器通过定位填充输入字段ng-model="data.person"?
我理解Angular依赖注入如何与指令一起使用,但需要澄清某些内容.我有一个虚拟测试指令如下:
app.directive("test", [
function() {
return {
restrict: "E",
scope: {},
controller: ["$scope", "$filter",
function($scope, $filter) {
var food = ["Apple pie", "Apple cobler", "Banana Split", "Cherry Pie", "Applesauce"];
$scope.favorites = $filter('filter')(food, "Apple");
}
],
template: "<div>{{favorites}}</div>"
}
}
]);
Run Code Online (Sandbox Code Playgroud)
这工作正常,将按food预期过滤数组.但是我注意到如果我$filter在指令中注入服务如下,它仍然有效:
app.directive("test", ["$filter",
function($filter) {
return {
restrict: "E",
scope: {},
controller: ["$scope",function($scope) {...
Run Code Online (Sandbox Code Playgroud)
我的问题:将服务注入声明行中的指令是更好的做法,如下所示:
app.directive("test", ["$filter", function($filter) {
或者像这样的控制器行:
controller: ["$scope", "$filter", function($scope, $filter) {?
没有区别吗?这是指令代码的Plunker.
我有以下几行:
var app = angular.module('app', []);
app.constant("const", {
foo: "bar",
test: "123"
});
Run Code Online (Sandbox Code Playgroud)
我现在想constant直接从模块中访问信息app.像这样的东西不起作用:
console.log(app.constant.const.foo);
Run Code Online (Sandbox Code Playgroud)
我知道你可以将常量注入控制器,服务等并访问信息,但出于我的目的,我想直接从模块中访问它们.
访问已注册的唯一方法constant是将其注入控制器/服务/等?