<!DOCTYPE html>
<html ng-app='myApp'>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('x', function (ser) {
alert(ser.val);
});
app.service('ser', function () {
var obj = {};
obj.val = 'xyz';
return obj;
})
</script>
<body>
<h1 ng-controller='x'>My First JavaScript Animation</h1>
</body>
</html>
我们可以在对象上创建并给出对象属性并在工厂中返回它,但是service是一个构造函数,但是在程序中我做的与我在工厂中应该做的一样,创建并返回一个宾语。 不是应该使用这个关键字来完成......但是它正在工作吗?
答案 0 :(得分:3)
我提到了这个链接: [https://www.airpair.com/angularjs/posts/top-10-mistakes-angularjs-developers-make][1]
根据该链接:
这些名称在开始时几乎对每个AngularJS开发人员造成混淆。他们真的不应该因为他们(几乎)是同一件事的语法糖! 以下是AngularJS来源的定义:
public abstract class TraceStream extends PrintStream
{
private static final Logger trace = LogManager.getLogger(TraceStream.class);
public TraceStream(OutputStream stream) {
super(stream);
}
protected abstract void trace(String line);
public void print(boolean b) {
TRACE.info(String.valueOf(b));
}
public void print(char c) {
trace(String.valueOf(c));
}
public void print(char[] s) {
trace(String.valueOf(s));
}
public void print(double d) {
trace(String.valueOf(d));
}
public void print(float f) {
trace(String.valueOf(f));
}
public void print(int i) {
trace(String.valueOf(i));
}
public void print(long l) {
trace(String.valueOf(l));
}
public void print(Object obj) {
trace(obj.toString());
}
public void print(String s) {
trace(s);
}
public void println() {
trace("");
}
public void println(boolean x) {
trace(String.valueOf(x));
}
public void println(char x) {
trace(String.valueOf(x));
}
public void println(char[] x) {
trace(String.valueOf(x));
}
public void println(double x) {
trace(String.valueOf(x));
}
public void println(float x) {
trace(String.valueOf(x));
}
public void println(int x) {
trace(String.valueOf(x));
}
public void println(long x) {
trace(String.valueOf(x));
}
public void println(Object x) {
trace(x.toString());
}
public void println(String x) {
trace(x);
}
public void write(int c) {
trace(String.valueOf(c));
}
public static void traceStdErr() {
System.setErr(new TraceStream(System.err) {
protected void trace(String line) {
TRACE.error(ThreeLetterCode.CODE_EXC, line);
}
});
}
public static void traceStdOut() {
System.setOut(new TraceStream(System.out) {
protected void trace(String line) {
TRACE.info(ThreeLetterCode.CODE_INF, line);
}
});
}
}
从源代码可以看到,服务只调用工厂函数,而工厂函数又调用提供者函数。