角度控制器语法

时间:2016-09-28 22:37:13

标签: angularjs controller

在回顾角度访谈question and answers时,我注意到作者编写了控制器的语法如下:

TypeErrorTraceback (most recent call last)
<ipython-input-40-ab9e3025f679> in <module>()
----> 1 time_df = time_rdd.toDF(['my_time'])

/usr/local/spark-latest/python/pyspark/sql/session.py in toDF(self, schema, sampleRatio)
     55         [Row(name=u'Alice', age=1)]
     56         """
---> 57         return sparkSession.createDataFrame(self, schema, sampleRatio)
     58 
     59     RDD.toDF = toDF

/usr/local/spark-latest/python/pyspark/sql/session.py in createDataFrame(self, data, schema, samplingRatio)
    518 
    519         if isinstance(data, RDD):
--> 520             rdd, schema = self._createFromRDD(data.map(prepare), schema, samplingRatio)
    521         else:
    522             rdd, schema = self._createFromLocal(map(prepare, data), schema)

/usr/local/spark-latest/python/pyspark/sql/session.py in _createFromRDD(self, rdd, schema, samplingRatio)
    358         """
    359         if schema is None or isinstance(schema, (list, tuple)):
--> 360             struct = self._inferSchema(rdd, samplingRatio)
    361             converter = _create_converter(struct)
    362             rdd = rdd.map(converter)

/usr/local/spark-latest/python/pyspark/sql/session.py in _inferSchema(self, rdd, samplingRatio)
    338 
    339         if samplingRatio is None:
--> 340             schema = _infer_schema(first)
    341             if _has_nulltype(schema):
    342                 for row in rdd.take(100)[1:]:

/usr/local/spark-latest/python/pyspark/sql/types.py in _infer_schema(row)
    987 
    988     else:
--> 989         raise TypeError("Can not infer schema for type: %s" % type(row))
    990 
    991     fields = [StructField(k, _infer_type(v), True) for k, v in items]

TypeError: Can not infer schema for type: <type 'float'>

我习惯以这种方式编写控制器:

function Customer($scope)
{
        $scope.CustomerName = "Shiv";
        $scope.CustomerCode = "1001";
        $scope.Add = function () {
        }
        $scope.Update = function () {
        }
}

如何在角项目中使用作者控制器语法???

3 个答案:

答案 0 :(得分:1)

John Papa是Angular上全国公认的“大师”,他建议在第一个例子中创建控制器。有关用法,请参阅HERE

答案 1 :(得分:0)

app.controller(&#39;顾客&#39;,顾客($范围))

答案 2 :(得分:0)

作者只是使用命名函数,而不是像你那样直接传递匿名函数。

app.controller('Customer',Customer);

function Customer($scope) {
  ...
}

此外,我建议查看依赖注入,这是其严格模式所需的Angular最佳实践。像这样......

app.controller('Customer',Customer);

// Dependencies declared here
// This tells angular what to inject into your controller
Customer.$inject = ['$scope'];

// Controller Constructor Function
// You can now safely use $scope.
function Customer($scope) {
  ...
}
  

结帐Using StrictDI in Angular docs。在底部有一个关于BadController,GoodController1和GoodController2的JS示例

     

第一个 GoodController1 将改善您的风格。作者选择 GoodController2 语法