AngularJS从控制器错误调用服务

时间:2017-11-14 19:22:05

标签: angularjs

我不确定为什么会收到此错误,有人可以向我解释一下吗?

  

TypeError:无法读取属性' getValueByName'未定义的       在对象。 (http://localhost:63342/Hardcore%20Gaming%20JS/scripts/MainApp.js:175:32)       at Object.invoke(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:4473:17)       在extend.instance(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:9093:34)       at nodeLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:8205:36)       在compositeLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:7637:13)       在publicLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:7512:30)       在链接(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.js:986:7)       at invokeLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:8746:9)       at nodeLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:8246:11)       在compositeLinkFn(https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:7637:13)   

我创建了一项服务,以便存储,更新和返回零件清单的值。我在点击不同的单选按钮后尝试更新partsList。

此外,如果您知道如何通过不同的视图更改来保持这些选择,这些更改也很棒。

谢谢!

这是我的服务代码:

mainApp.controller('controller_Computer_Type', ['$scope', function ($scope, MyService) {
    //Computer Type Selected
    $scope.current = MyService.getValueByName("selected_computer_type");

    //Updates the partsList
    $scope.change = function () {
        var value = $scope.current.toString();
        MyService.setValue("selected_computer_type", value);
    };

    //List of options for computer type
    $scope.types = [
        //TODO: update url images to reflect personal gaming and business profiles
        {name: 'Personal', value: 'personal', url: 'images/intel 700/ABFS_1_201706141903610053.jpg'},
        {name: 'Gaming', value: 'gaming', url: 'images/amd 500/83-230-100-02.jpg'},
        {name: 'Business', value: 'business', url: 'images/intel 500/83-101-369-z01.jpg'}
    ];
}]);

这是我的控制器代码:

<div ng-controller="controller_Computer_Type">

    <h2>Type of Computer</h2>

    <!--Radio Button Options Using Images-->
    <div class="row">
        <label class="col-sm-4" ng-repeat="option in types">
            <input type="radio" name="radio_type" ng-model="$parent.current" ng-change="change()" value="{{option.value}}" hidden/>
            <img class="img-thumbnail" src="{{option.url}}" alt="{{option.value}}">
            <img class="overlay" src="images/greenCheck.png">
        </label>
    </div>

    <!--Debugging purposes-->
    <p>Current RB - {{current}}</p>

    <p>Current DD - {{selectedItem.name}}</p>

    <p>Message - {{message}}</p>

</div>

这是我的观看代码:

{{1}}

1 个答案:

答案 0 :(得分:1)

你想念这个

mainApp.controller(&#39; controller_Computer_Type&#39;,[&#39; $ scope&#39;,&#39; MyService &#39;,功能($ scope,MyService) )