Search code examples
javascriptangularjscontrollerumbraco

Config is undefined for $scope.model property


I am implementing a controller for a content picker in Umbraco 7, where I need to change the start node to match a specific content node. However wen I load up the page with the content picker I receive an error saying:

"Cannot read property 'config' of undefined"

In relation to this piece of code:

$scope.model.config.StartNodeId = 1083;
     if ($scope.model.config.StartNodeId) {
          options.startNodeId = $scope.model.config.StartNodeId;
     }

My entire controller:

angular.module("umbraco").controller("UIOMatic.FieldEditors.Pickers.ContentController",
    function ($scope, $routeParams, $http, dialogService, entityResource, iconHelper) {

        function init() {
            if (!$scope.setting) {
                $scope.setting = {};
            }

            var val = parseInt($scope.property.value);

            if (!isNaN(val) && angular.isNumber(val) && val > 0) {
                $scope.showQuery = false;

                entityResource.getById(val, "Document").then(function (item) {
                    item.icon = iconHelper.convertFromLegacyIcon(item.icon);
                    $scope.node = item;
                });
            }

            $scope.openContentPicker = function () {
                var d = dialogService.treePicker({

                    section: "content",
                    treeAlias: "content",
                    multiPicker: false,
                    callback: populate
                });
            };

            $scope.model.config.StartNodeId = 1083;
            if ($scope.model.config.StartNodeId) {
                options.startNodeId = $scope.model.config.StartNodeId;
            }

            $scope.clear = function () {
                $scope.id = undefined;
                $scope.node = undefined;
                $scope.property.value = undefined;
            };

            function populate(item) {
                $scope.clear();
                item.icon = iconHelper.convertFromLegacyIcon(item.icon);
                $scope.node = item;
                $scope.id = item.id;
                $scope.property.value = item.id;
            }
        };

        if ($scope.valuesLoaded) {
            init();
        } else {
            var unsubscribe = $scope.$on('valuesLoaded', function () {
                init();
                unsubscribe();
            });
        }
    });

I tried changing the start node ID to 1083, which is what I want, and I can open the content picker just fine, but it won't allow me to save my changes. It also allows for multi-picking, which I have set to false in my config object.

This is the documentation of the content picker from the author: http://uiomatic.readthedocs.io/en/stable/02.DefaultEditorViews/#content-picker


Solution

  • I think You should initialize $scope.model before assigning value to its object. use

    $scope.model = {}