2015-05-05 365 views
3

我與JS角小的項目工作(PS:這是我第一次使用它),我的這個錯誤阻止,我不明白爲什麼角JS項目不工作

angular.module("jsonerator", []) 
.controller('theController', function ($scope) { 

    $scope.personld = { 
     "@context": { 
      "schema": "http://schema.org/" 
     }, 
     "@graph": [ 
      { 
       "@id": "person", 
       "@type": "schema:Person", 
      } 
     ] 
    } 

    $scope.personld["@graph"][0]["schema:givenName"] = ""; 
    $scope.personld["@graph"][1]["schema:familyName"] = ""; 
} 
+0

*「被此錯誤阻止」* ..什麼錯誤? – dfsq

+0

oh sorry0thsi one:「無法設置屬性'架構:未定義的familyName' – user3430205

+1

您在數組''@graph」'中只有一個元素。所以'$ scope.personld [「@ graph」] [1]'確實是未定義的。你可能想要'$ scope.personld [「@ graph」] [0] [「schema:familyName」] =「」' – dfsq

回答

2

要修復錯誤,你需要添加另一個對象到你的@graph數組中,它應該可以工作。然後將定義["@graph"][1]。問題是你只有一個項目在你的數組中。

"@graph": [ 
    { "@id": "person", "@type": "schema:Person" }, 
    {} 
] 
+0

它的作品!謝謝 – user3430205

+0

沒有probs,很高興提供幫助 –

1

您的@graph數組長度只有1個元素,所以$scope.personld["@graph"][1]不存在,並且命令$scope.personld["@graph"][1]["schema:familyName"] = "";會引發錯誤。