简体   繁体   中英

Changing routes doesn't refresh data, gives ng:areq error

In my AngularJS app, I have a route that looks like:

.when('/:project/:page', {
    templateUrl: '/ui-editor/editor',
    controller: 'EditorCtrl'
})

In a view, I have a link that looks like:

<a href="/{{currentProjectId}}/{{currentPageId + 1}}">Next page</a>

When I click that link in my app, the URL in the browser changes correctly (eg /myproject/1 to /myproject/2 ), but I get an error Error: [ng:areq] Argument 'scope' is required . The line that the error refers to is the console.log statement in:

socket.on('ws:init-data', function(data) {
    console.log('on ws:init-data', data);
    // Handle incoming data from server
    //...
});

If I refresh my browser, the correct data loads as it should for /myproject/2 .

use ng-href is better. It can avoid link be clicked before angular parse.

<a ng-href="/{{currentProjectId}}/{{currentPageId + 1}}">Next page</a>

I don't know the exact cause about this error, but I have a suggestion:

use $location to change your url.

you can see on the dev_guide:

When should I use $location?

Any time your application needs to react to a change in the current URL or if you want to change the current URL in the browser.

http://docs.angularjs.org/guide/dev_guide.services.$location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM